Call to a member function connection() on a non-object error on Laravel 5 Call to a member function connection() on a non-object error on Laravel 5 laravel laravel

Call to a member function connection() on a non-object error on Laravel 5


While running TestCase in Laravel 6.x and above, I often encounter this error. I realized that the unit test is extended from PHPUnit TestClass rather than Laravel TestCase. So change PHPUnit\Framework\TestCase; to Tests\TestCase.

<?phpuse Tests\TestCase;class ArtifactTest extends TestCase{}


go to bootstrap/app.php

Just uncomment

$app->withEloquent();


Working with models in your settings files is not the best way to use them.

Your problem is, that your model query started BEFORE Laravel started it's services. That's why, when you try to make your query, model can't resolve it's connection, because DB service hasn't been initiated.

If you want do this stuff, create your own ServiceProvider and update your config there, or do it right in the boot method of your existing AppServiceProvider.