7 months ago

Auth::user() available in view but not controller?

Posted 7 months ago by lauriek

I have an old Laravel 5.0 project which I'm trying to move to L5.5 (for now) - I have set up a fresh vm and a fresh install of L5.5. I have created the auth scaffolding with Artisan, and the routes and views and stuff were set up (db tables are there and a couple of users seeded).

I'm now trying to pull the old site code into this new environment and I have a weird issue where Auth::user() is available and populated in view code, but not in my controller.

I have this code in a method in my base controller (which all other controllers extend)

$user = (Auth::check()) ? Auth::user() : false; View::share('authuser', $user);

But if I dump that variable it is empty (bool false) The Auth facade is being imported in this file.

However in a view I have this

@if(Auth::check()) ... {{ Auth::user()->name }}

Which correctly outputs my logged in username.

So how come Auth::user() is not available in my controller code?

For clarity I do not think I have pulled over any code relating to Auth from the old 5.0 site, definitely not the old controllers or auth views

Please sign in or create an account to participate in this conversation.