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

isimmons's avatar

4.3 auth class call to member function connection on non-object

Anyone else trying to move Larabook over to 4.3 and having this issue?

It seems to be the Auth class. If I comment out the Auth::attempt part in SessionsController@store it successfully goes to the statuses url. But then because of the auth filter on the statuses route I get the same error. If I remove the auth filter from the route it goes through just fine. Then in the first line of StatusesController@index where we pass through Auth::user() I get an error because it is returning NULL.

So something is definitely wrong with Auth or my User model.

I've changed out the new contracts for interfaces according to the example User model that comes with 4.3 and made sure config/auth.php has the correct namespace to the model.

0 likes
8 replies
bashy's avatar

Try do some debugging?

Auth::loginUsingId(1);

Or something else like that to narrow down where/what is failing

1 like
isimmons's avatar

Ok yesterday at one point I thought I had a issue with Redirect and then decided it was not the issue but that was because I was fixing multiple issues but now I think it is a problem with Redirect. I created the following test routes and as you can see the error only happens when using a Redirect. Even redirecting home or to test2 where there is no DB connection throws the same error. "member function connection on a non-object"

Route::get('test', function()
{
    if(Auth::loginUsingId(1))
    {
        return 'foo'; //works
        //return Redirect::to('statuses'); // gets error
        //return Redirect::to('home'); //gets error
        //return Redirect::to('test2'); //gets error
    }
});

Route::get('test2', function()
{
    return 'bar';
});

I also get a 500 Internal Server Error in chrome dev tools

Remote Address:127.0.0.1:8000
Request URL:http://larabook.dev:8000/test2
Request Method:GET
Status Code:500 Internal Server Error

Wish I could edit the title of this post now.

isimmons's avatar

I tried to do this as a new application rather than an upgrade. But I'll look through the upgrade and maybe see something else I need to change in my models or other services. I think it's a bug with Redirect though since it's a new app and those test routes fail on Redirect. Looks like some updates were made today so I'll do a composer update and also create a separate new app with nothing but test routes like above. If it's still happening I'll submit an issue.

Thanks

ovvessem's avatar

Have you tried the new function return redirect('foo/bar'); instead of Redirect:: ?

isimmons's avatar

I didn't know about that function so thanks but no luck there either.

I did some more testing and it's weird but it seems like it's a combination of Auth and Redirect. With the test routes above hitting the Auth class and returning 'foo' works but hitting the Auth class and returning a redirect does not. Commenting out the Auth part and simply doing the redirect also works. It's as though something is going on when the session is created in between hitting the auth class and redirecting.

Same thing with the auth filter on statuses. If I change return Redirect::guest('login') to return 'foo'; it works. Otherwise it gives the connection error.

Also I have to delete the session from storage/sessions in between each test

isimmons's avatar

Made some progress narrowing it down. Auth seems to be working fine and it's not really the redirect class causing the issue.

If I run the following test route once it works

Route::get('test', function()
{
    if(! Auth::check())
    {
        if(Auth::loginUsingId(1))
        {
            //return 'foo'; //works
            return Auth::user(); // works
            //return Redirect::to('test2'); //gets error
        }
    }

    return Auth::user();

});

If I hit it a second time I get the error. If I manually delete the session in storage/sessions and then hit that url again, it works again.

So the best I can tell is that something is wrong with the session being created which happens before or during the redirect. If I don't do a redirect but just refresh the page hitting the url I guess it's the same as doing a redirect as far as what ever is wrong with the session.

I created a new l43 app and only copied over my User model, migrated the database, and used the same test route and it works perfectly in that one so I just can't figure out what is wrong here. All I can think to do now is trash it and start again.

isimmons's avatar
isimmons
OP
Best Answer
Level 17

Yay problem solved! It was a Auth problem but was because of the AppServiceProvider with View::share($currentUser, Auth::user()); trying to load before the Illuminate service providers. Probably this one specifically 'Illuminate\Database\DatabaseServiceProvider'

Found the answer here https://laracasts.com/discuss/channels/requests/laravel-43-and-viewshare

Just moved the entire application service providers block below the block of Illuminate service providers.

Please or to participate in this conversation.