wp-orm icon indicating copy to clipboard operation
wp-orm copied to clipboard

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

Open rafaucau opened this issue 1 year ago • 2 comments

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 avatar May 13 '24 17:05 rafaucau

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 );

rafaucau avatar May 15 '24 11:05 rafaucau

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 avatar May 15 '24 19:05 dimitriBouteille