I am using Lumen 5.3.1. $app->withFacades()
and $app->withEloquent()
have been uncommented in app.php
. In web.php
I run the following code:
$app->get('foo', function () {
return app('db')->select("SELECT * FROM foo");
return "Connected successfully to database " . DB::connection()->getDatabaseName();
});
The select()
call correctly returns data from the foo
table. However, DB::connection()
returns:
FatalErrorException in Manager.php line 74:
Call to a member function getConnection() on null
Why does one work but not the other?
I'd say double check your service providers. It looks like you are going through the DB Capsule, when actually that's intended for use out of Laravel/Lumen. Anyway if you are in fact using the Capsule Manager, you probably have to register it in a boot
method of the provider, not register
.
Also, in order to find out more about what's going on, add this to your test code:
dd(app('db'), DB::getFacadeRoot());
Share the result if you want, this will give more information about the difference between the two methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With