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

TheFriendlyHacker's avatar

Auth::user() vs $request->user() ?

It seems as though you can retrieve the current authenticated user via the Auth facade or via the Request...

$user = $request->user();
// or...
$user = Auth::user();

Is there a difference between these methods? Or is it pretty much the same?

0 likes
17 replies
SaeedPrez's avatar

Don't forget the helper function auth()->user(), my favorite ☺

I guess you could say they are different ways to get the same thing.

9 likes
jekinney's avatar

Unfortunately the advantages of one or the other depends. The request, from my understanding logging queries, doesn't run a query but may have stale data. I know auth does query the db on each request, though it does it anyways.

Also a more fluent/readable approach instead of having a random auth()->user() in an array of $requests seems out of place.

5 likes
Ssempebwa's avatar

I am experiencing a problem where

 Auth::user();

returns user A from a browser firefox, even when its incognito. Yet i am using another chrome incognito to view another user account logged lets say user B but still returns user A.

However when i use

$request->user();

in the same place as the first instance i get expected results. Two different users in the respective browsers above.

I have done a check experiment by using Microsoft edge to log into a third user account. The results still hold as i switch the code blocks above.

1.What causes this problem or is this feature or am i miss using the tool?

1 like
dawood's avatar

$request->user() returns an instance of the authenticated user.

6 likes
saeeddirect's avatar

@martinbean Value is value, I dont understand why do you have problem with that ? have seen you doing it alot.

10 likes
Snapey's avatar

@saeeddirect where would we be if everyone started posting bits from the documentation on every old post? This thread is actually 5 years old and contains dated information.

1 like
saeeddirect's avatar

@Snapey I don't think that it contains dated information. Someone who lands on this page from google would find it helpful. Laravel docs for 9.x say:

Alternatively, once a user is authenticated, you may access the authenticated user via an Illuminate\Http\Request instance. Remember, type-hinted classes will automatically be injected into your controller methods. By type-hinting the Illuminate\Http\Request object, you may gain convenient access to the authenticated user from any controller method in your application via the request's user method

Link: https://laravel.com/docs/9.x/authentication#:~:text=Alternatively%2C%20once%20a,method%3A

6 likes
martinbean's avatar

@saeeddirect Because people keep bumping ~5 year old threads with stupid responses like, “did you find answer?”, “I have same issue”, etc. There’s absolutely no value in replies like that.

1 like
saeeddirect's avatar

@martinbean yes, "dawood" actually answered correctly just in case so that people won't post "did you find the answer". peace.

1 like
Snapey's avatar

@saeeddirect The question was

Is there a difference between these methods? Or is it pretty much the same?

"dawood" just stated one of the options === waste of time

3 likes
martinbean's avatar

@22289d And you wasted my time by tagging me and making me read that completely useless reply. So let’s call it even 🙃

1 like
woodgas's avatar

Hello! I have made some tests in an empty Laravel project. Laravel version is 9.30.

If the user is authenticated the Laravel determines it by session and makes an SQL query like:

SELECT * FROM `users` WHERE `id` = 12 LIMIT 1

The SQL query executes in any way, by default, under the hood. The Laravel creates User object. The request()->user(), auth()->user() and Auth::user() just return the same User object without additional queries to the DB. In example Auth::user() === request()->user() returns true etc.

So feel free to choose the method.

3 likes

Please or to participate in this conversation.