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

Garet's avatar
Level 3

In a Job, Auth::user() doesn't work, but request()->ip() does! Why?

I've moved some of my functionality to jobs, in particular sending notifications and emails.

One thing I noticed right away is that in the handle() method (and subsequent blade view) Auth::user() returns null. This makes sense, because the code is passed away to the job and processed later on, so the job has no notion of a currently authenticated user.

However, request()->ip() does appear to be working and returns the IP address of the user who was responsible for invoking the job. How can this be? Similarly to Auth::user() I would have expected the job to not know what the IP address was for the user.

0 likes
6 replies
Snapey's avatar

if you are executing your jobs sync then they are still executed in the same request cycle and the global request helper is still working with the same request

Garet's avatar
Level 3

That was my first thought, so then I switched from sync to database and tested it, and request()->ip() still works and returns the original user's IP address!

Snapey's avatar

It does not matter how the job is stored, its about when the job is serviced.

Are you queuing the jobs and then running them with php artisan queue:work? If not, then everything is being done under the web request.

Garet's avatar
Level 3

For testing I was manually running php artisan queue:work but then later on I setup Supervisor and the results are the same

Snapey's avatar

I don't know then. That does not make any sense. There won't even be a request object.

Garet's avatar
Level 3

Yep, this is why I'm puzzled

Please or to participate in this conversation.