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

socieboy's avatar

I can't get the auth user.

Best way to get access to the Auth::User() on every single request...

0 likes
51 replies
mstnorris's avatar

@socieboy what do you mean? When submitting forms? When accessing routes?

What is wrong with auth()->user()? If you are referring to needing access to details in all views, then use View Composers, but until you are a little more explicit in what you actually want, we're all just guessing.

mstnorris's avatar

Why didn't you say so ;)

So what are you trying to do then?

absiddiqueLive's avatar

@socieboy Follow this way

Route::group(['middleware' => 'auth'], function () {  //this auth is laravel default auth
    Route::get('home', 'Users\DashboardController@index');
    Route::resource('user', 'Users\DashboardController');
});

OR direct access any where

Auth::guest();
Auth::user();
mstnorris's avatar

Please can you clarify what you mean by request, as the term is used in various contexts.

JohnRivs's avatar

Maybe store the current user in a controller class property.

use Illuminate\Auth\Guard;

class MyController extends Controller {
    
    protected $currentUser;

    public function __construct(Guard $auth)
    {
        $this->currentUser = $auth->user();
    }
absiddiqueLive's avatar

use below code in your constructor

if(Auth::guest()){
    //logic
}else{
    //logic
}
socieboy's avatar

Each time the user change a page or reload the page on the app i want to get the auth user or null if it's guest, then i want to store the id of the user or null if it's guest as i said in my database.

i try on routes.php file

Foo::do_it();

class Foo

class Foo{

    public function do_it()
    {
        dd(auth()->user());
    }
}
mstnorris's avatar

@socieboy that is the first time you have mentioned anything about a database. I'm sure I have like you to the Guidelines for posting on Laracasts.com before but in case you haven't seen them I suggest taking a look over them, as long conversations like this can be saved if you give us more information up front.

socieboy's avatar

@mstnorris sorry if i made you waste your time! your post about Guidelines should be always on the top! I didn't know about that post! ok!

JohnRivs's avatar

@socieboy well, if the user makes a request to a route where the response is defined by a closure, you can do a similar thing. Can you show me a request example where it doesn't catch the auth user?

mstnorris's avatar

@socieboy it is in the sidebar under the most recent lesson. You're not wasting anyone's time but your own. It is just so we can help you better, if you give us as much information as possible up front. I'm only trying to help.

mstnorris's avatar

From what I understand you want a way to track the user (whether they are logged in or a guest) and store that in a database, am I right?

JohnRivs's avatar

@socieboy offtopic: why are you using exclamation points for each sentence? Are you that excited?

Why don't you just reference Auth::user() whenever you actually need to interact with the user instance?

mstnorris's avatar

@socieboy don't be short. This is the title of your question "I can't get the auth user.". It doesn't mention anywhere about saving a user's details to the database upon each request. I appreciate you did finally mention it 8 posts ago, I was merely trying to clarify that it was indeed what you wanted as that changes what solutions we can give you. Rather than just saying "no", provide more details about what you want.

gwp's avatar

If you really want to do this... You could put this logic in a controller class that you extend from for each of your controllers. Put the logic in the __construct function like this:

<?php

class My_Custom_Controller extends Base_Controller
{
    public function __construct()
    {
        if (Auth::user()) {
        #save Auth::id() to database
        } else {
        # save null to database
        }

    #or like this
        $toSaveInDB = (Auth::user()) ? Auth::id() : null;
        #then save $toSAveInDB to... the db
    }
}

Then you can use it like:

class Pages_Controller extends My_Custom_Controller {
}
JohnRivs's avatar

@mstnorris is kinda right. Also, the title is misleading; yes you can get the auth user, you're asking about how to get it in a certain way though.

@gwp approach is slightly better than mine; instead of storing the auth user in a property inside each controller, do it once in the base controller.

2 likes
mstnorris's avatar

Create a BaseController that your normal UsersController, ArticlesController, PagesController or whatever extend so that the functionality that you're after is in each "request".

Controller Controller

class BaseController extends Controller {
    public function __construct() {
        if ( auth()->user() ) {
            // save auth()->user()->id to database
        } else { 
            // save null to database (bear in mind you will have a LOT of nulls
        } 
    }
}

BaseController Controller

class UsersController extends BaseController {
    // standard stuff here
}
1 like
socieboy's avatar

I don't personally give all details of what I doing but i will this time and I hope i can get a good feedback, no things like ... why your title is like that or whatever! I won't explain all thing in the title!

This is a Chat package for Laravel applications, I try to do it as easy is possible for other users.

I have my folder with my package with my own routes.php file.

I have added to the session table the field user_id, wish is nullable.

So now i need to update the user_id every time the users moved on the app, the Chat package could be install over different apps or websites, so I can't place the logic to get the auth user on the Controller class, I need other way?

@mstnorris do you get the idea now?

Next

Please or to participate in this conversation.