Class db does not exist eloquent
A facade root has not been set
and other terrible errors.
This post for those who like me using Eloquent outside Laravel.
With config like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
use Devio\Eavquent\Agnostic\BootEavquent; use Illuminate\Database\Capsule\Manager as Capsule; use Illuminate\Events\Dispatcher; use Illuminate\Container\Container; use Illuminate\Support\Facades\Schema; $capsule = new Capsule; $capsule->addConnection([ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'dbname', 'username' => 'user', 'password' => 'password', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ]); // Set the event dispatcher used by Eloquent models... (optional) $capsule->setEventDispatcher(new Dispatcher(new Container)); // Make this Capsule instance available globally via static methods... (optional) $capsule->setAsGlobal(); // Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher()) $capsule->bootEloquent(); |
Sometime need to log queries or just count it.
Almost all google results show code like this:
1 |
DB::listen(function($query){}); |
But outside Laravel you will get errors written above.
I spent some time to get it work.
And now i got solution 😀
To get work query logs outside Laravel use this 🙂
1 2 3 4 5 6 7 8 9 10 |
// Listening to all queries $capsule->connection()->listen(function ($query) { var_dump($query); }); // Enabling query log $capsule->connection()->enableQueryLog(); // Getting array of executed queries $capsule->connection()->getQueryLog(); |