Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lumen call to DB::connection() returns null even though select() is successful

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?

like image 809
ebakunin Avatar asked Nov 06 '16 21:11

ebakunin


1 Answers

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.

like image 65
alepeino Avatar answered Nov 05 '22 20:11

alepeino