Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add Illuminate\Support\Facades\DB facade #64

Open
rafaucau opened this issue May 13, 2024 · 2 comments
Open

[FEATURE] Add Illuminate\Support\Facades\DB facade #64

rafaucau opened this issue May 13, 2024 · 2 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request v4
Milestone

Comments

@rafaucau
Copy link
Contributor

rafaucau commented May 13, 2024

I'm encountering an issue when trying to use DB::raw. The error message indicates a problem with the facade root not being set.

//...
use Illuminate\Support\Facades\DB;
//...
TestStat::upsert(
	[[
		'test_id' => $test_id,
		'count' => 1,
	]],
	[ 'test_id' ],
	[ 'count' => DB::raw( 'count + 1' ) ]
);

PHP Fatal error: Uncaught RuntimeException: A facade root has not been set. in /var/www/vhosts/localhost/vendor/illuminate/support/Facades/Facade.php:352

How can I properly use DB::raw in WordPress context to enable raw SQL expressions?

@rafaucau rafaucau added the bug Something isn't working label May 13, 2024
@rafaucau rafaucau changed the title [QUESTION]: How to use DB::raw? [QUESTION/BUG]: How to use DB::raw? May 13, 2024
@rafaucau rafaucau changed the title [QUESTION/BUG]: How to use DB::raw? [QUESTION/BUG]: How to use DB::raw? Uncaught RuntimeException: A facade root has not been set. May 13, 2024
@rafaucau rafaucau changed the title [QUESTION/BUG]: How to use DB::raw? Uncaught RuntimeException: A facade root has not been set. [QUESTION/BUG]: How to use DB::raw? / Uncaught RuntimeException: A facade root has not been set. May 13, 2024
@rafaucau
Copy link
Contributor Author

rafaucau commented May 15, 2024

Workaround:

use Dbout\WpOrm\Orm\Database;

Database::getInstance()->raw( 'count + 1' );

Or in a mu-plugin add facade to use DB:

use Dbout\WpOrm\Orm\Database;
use Illuminate\Container\Container;
use Illuminate\Support\Facades\Facade;

$container = new Container();
$container->instance( 'db', Database::getInstance() );
Facade::setFacadeApplication( $container );

@dimitriBouteille
Copy link
Owner

Hi @rafaucau , thank you for reporting this.

The facades are unfortunately not supported by the library since it is a few things that are mainly used by Laravel projects.

I will try to add these facades to the project but I am not sure it is possible since you have to go through the Illuminate container system. Unfortunately, this requires adding dependencies to projects:(

The only solution today is indeed what you propose in your message.

@dimitriBouteille dimitriBouteille added the enhancement New feature or request label May 15, 2024
@dimitriBouteille dimitriBouteille changed the title [QUESTION/BUG]: How to use DB::raw? / Uncaught RuntimeException: A facade root has not been set. [FEATURE] Add Illuminate\Support\Facades\DB facade May 15, 2024
@dimitriBouteille dimitriBouteille added this to the 4.x.x milestone May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request v4
Projects
None yet
Development

No branches or pull requests

2 participants