Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ralphmrivera's avatar

Call to a member function connection() on null

Hi everyone,

I am facing a challenge that I'm not sure what to do with. I have a bunch of pages that work, but periodically I get this error:

Call to a member function connection() on null

The full error is below. When I mentioned "periodically" above, what I meant is that if I publish a page and repeatedly click the refresh button, the error below wth generate roughly 1 out of 20 times. I'm including my route and the complete blade below as well.

Any insight would be greatly appreciated.

Whoops, looks like something went wrong.

1/1
FatalErrorException in Model.php line 3245:
Call to a member function connection() on null
in Model.php line 3245
at FatalErrorException->__construct() in HandleExceptions.php line 133
at HandleExceptions->fatalExceptionFromError() in HandleExceptions.php line 118
at HandleExceptions->handleShutdown() in HandleExceptions.php line 0
at Model::resolveConnection() in Model.php line 3211
at Model->getConnection() in Model.php line 1914
at Model->newBaseQueryBuilder() in Model.php line 1857
at Model->newQueryWithoutScopes() in Model.php line 1831
at Model->newQuery() in IlluminateReminderRepository.php line 165
at IlluminateReminderRepository->removeExpired() in SentinelServiceProvider.php line 427
at SentinelServiceProvider->sweep() in SentinelServiceProvider.php line 413
at SentinelServiceProvider->garbageCollect() in SentinelServiceProvider.php line 47
at SentinelServiceProvider->boot() in Container.php line 503
at call_user_func_array:{/Users/Bespin/Projects/Ewok/vendor/laravel/framework/src/Illuminate/Container/Container.php:503}() in Container.php line 503
at Container->call() in Application.php line 734
at Application->bootProvider() in Application.php line 717
at Application->Illuminate\Foundation\{closure}() in Application.php line 718
at array_walk() in Application.php line 718
at Application->boot() in BootProviders.php line 17
at BootProviders->bootstrap() in Application.php line 203
at Application->bootstrapWith() in Kernel.php line 222
at Kernel->bootstrap() in Kernel.php line 117
at Kernel->sendRequestThroughRouter() in Kernel.php line 87
at Kernel->handle() in index.php line 54
at {main}() in index.php line 0

My route

Route::get('/', function () {
    return view('test');
});

The blade (test.blade.php)

<!DOCTYPE html>
<html>
<head>
    <title>TITLE</title>
</head>
<body class="hold-transition skin-blue sidebar-mini">
    <div class="wrapper">
        <header class="main-header">
            {{-- Logo --}}
            <a href="#" class="logo">
                <span class="logo-mini">{!! Html::image('images/white.png', 'System', ['class' => 'img-responsive']) !!}</span>
                <span class="logo-lg">{!! Html::image('images/white.png', 'System', ['class' => 'img-responsive']) !!}</span>
            </a>
            <nav class="navbar navbar-static-top" role="navigation">
                {{--<!-- Sidebar toggle button-->--}}
                <a href="#" class="sidebar-toggle" data-toggle="offcanvas" role="button">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </a>
                <div class="navbar-custom-menu">

                </div>
            </nav>
        </header>

        {{--<!-- =============================================== -->--}}

        {{--<!-- Left side column. contains the sidebar -->--}}
        <aside class="main-sidebar">
            {{--<!-- sidebar: style can be found in sidebar.less -->--}}
            <section class="sidebar">
                {{--<!-- sidebar menu: : style can be found in sidebar.less -->--}}
                <ul class="sidebar-menu">
                        <li><a href="#"><i class="fa fa-dashboard"></i> <span> Dashboard</span></a></li>
                        <li><a href="#"><i class="fa fa-dashboard"></i> <span> Dashboard</span></a></li>
                        <li><a href="#"><i class="fa fa-dashboard"></i> <span> Dashboard</span></a></li>
                        <li><a href="#"><i class="fa fa-dashboard"></i> <span> Dashboard</span></a></li>
                        <li><a href="#"><i class="fa fa-dashboard"></i> <span> Dashboard</span></a></li>
                        <li><a href="#"><i class="fa fa-dashboard"></i> <span> Dashboard</span></a></li>
                </ul>
            </section>
            {{--<!-- /.sidebar -->--}}
        </aside>

        {{-- =============================================== --}}

        {{-- Content Wrapper. Contains page content --}}
        <div class="content-wrapper">
            {{-- Content Header (Page header) --}}
            <section class="content-header">
                <h1>
                    TEST
                </h1>
            </section>

            {{--<!-- Main content -->--}}
            <section class="content">
                Stuff
            </section>
            <div class="clearfix"></div>
            {{-- /.content --}}
        </div>
        {{-- /.content-wrapper --}}

        <footer class="main-footer">
            <div class="pull-right hidden-xs">
                <b>Version:</b> Beta 1.0.0
            </div>
        </footer>
    </div>
    {{-- ./wrapper --}}
</body>
</html>
0 likes
19 replies
coolpraz's avatar

comment or delete DB_CONNECTION in .env file on your laravel app root

matt@gdibass.com's avatar

Make sure you're including parent::setUp() in any setUp function you define. If you don't then the app won't be booted.

9 likes
Abrahamghaemi's avatar

Access denied for user database. check user and password try again

muhammad_zeshan's avatar

Make sure you don't use database connection in register() of any service provider. It should always go in boot() method as database connection not available to register() method.

6 likes
MosesNdeda's avatar

Awesome. This was my case in phpunit within an integration test.

raultm's avatar

thanks man! call to parent::setUp did the job

dbowlby's avatar

I ran into the same problem using Laravel 8.12.3. Add the following to the model class and you should be good to go.

use Illuminate\Database\Eloquent\Factories\HasFactory;

Reinier's avatar

@mingalevme: Thanks, this worked for me in Laravel 8

(So change PHPUnit\Framework\TestCase; to Tests\TestCase.)

19 likes
Scalable's avatar

Why Why Why

I wonder why this is a problem and why this is not fixed in parent branch.

But thanks. This seems to have fixed.

boxxroom's avatar

Had this same issue after running php artisan make:test ProfileTest --unit. Changing PHPUnit\Framework\TestCase; to Tests\TestCase; did indeed fix the issue.

I wonder the reasoning for unit tests to stub with PHPUnit\Framework\TestCase;

6 likes

Please or to participate in this conversation.