Laravel Auth::user() alternatives

How to get authenticated user instance in Laravel

Getting Authenticated user in Laravel

  • Using Facade:
  • Using helper function :

Note: Both of the above options do query database on each request!

  • In case we have Route::post() in our routes, our controller seems something like this:
Image for post
Image for post

Then inside the store action we can get the authenticated user via $request instance like:

Note: The request option does not run query on database on each request!

Checking logged in user:

We usually use check method in middleware

Getting Authenticated user id:

Simulating a login:

  • Inside a controller we can login a user with the user_id = $id
  • or

Note: false as the second argument means don’t tick remember_me when login a $user.

Inside Laravel Core:

How laravel core fetch the authenticated user when using can method in Authorization:

This is in Illuminate\Auth\AuthManager here

Image for post
Image for post

another case is happening here

Written by

Web geek, Self-taught full-stack web developer, Learning Python, Laravel, Vuejs, UX/UI design, Nuclear Physicist PhD

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store