wp-orm
wp-orm copied to clipboard
[FEATURE] Add Illuminate\Support\Facades\DB facade
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?
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 );
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.