Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Auth User ID in Laravel

If a user logs in Laravel 5.1 we can access user id

Auth::user()->id

In my previous app (not laravel) when a user logs in I'm registering a session for userid. And I was checking $_SESSION['user_id'] is available or not.

I want to ask that when i call Auth::user()->id is it generates and sql query for every request ? If it does it is not good for performance.

Should I register a new session like

Session::put('user_id', Auth::user()->id);

for performance.

Or Auth:user()->id is best choice ?

like image 319
Cihan Küsmez Avatar asked Nov 02 '15 21:11

Cihan Küsmez


People also ask

How can I get my Auth ID in Laravel API?

You can use auth()->user()->id or Auth::user()->id to get current user id. Show activity on this post. If you are trying that from custom made auth ( not default auth provided by laravel ), then all your auth required routes should be using web middleware.

How can I get username in Laravel 8?

$id = Auth::id(); // Retrieve the currently authenticated user's ID... $user = $request->user(); // returns an instance of the authenticated user... $id = $request->user()->id; // Retrieve the currently authenticated user's ID... $user = auth()->user(); // Retrieve the currently authenticated user...

How do you get auth session in Laravel?

You can use Auth::id() instead. This grabs it from the session. If one doesn't exist in the session, then it will run the query and grab it from the database. Yes it does.

What is auth () in Laravel?

Laravel includes built-in authentication and session services which are typically accessed via the Auth and Session facades. These features provide cookie-based authentication for requests that are initiated from web browsers. They provide methods that allow you to verify a user's credentials and authenticate the user.


2 Answers

You can use Auth::id() instead. This grabs it from the session. If one doesn't exist in the session, then it will run the query and grab it from the database.

like image 129
Thomas Kim Avatar answered Oct 12 '22 13:10

Thomas Kim


This is not an actual answer, but it intends to show how Auth::id() works. Add the following route to your web.php:

Route::get('/fake', function () {
  \DB::enableQueryLog();
  Auth::id();
  dump(\DB::getQueryLog());
});

Now, navigate to /fake, which results in the something like:

array:1 [▼
  0 => array:3 [▼
    "query" => "select * from `users` where `id` = ? limit 1"
    "bindings" => array:1 [▼
      0 => 284
    ]
    "time" => 15.37
  ]
]

I tried this over and over, and it seems that Auth::id() always queries the database, as opposed to somehow caching the result in the session (I'm using Laravel 5.6, PHP 7.2.3, and MariaDB 10.2.14).

To confirm, I also enabled MariaDB logging feature, and I got:

180611 12:08:10    77 Connect   user@localhost as anonymous on dbname
           77 Query use `dbname`
           77 Prepare   set names 'utf8mb4' collate 'utf8mb4_unicode_ci'
           77 Execute   set names 'utf8mb4' collate 'utf8mb4_unicode_ci'
           77 Close stmt    
           77 Prepare   set time_zone="+04:30"
           77 Execute   set time_zone="+04:30"
           77 Close stmt    
           77 Prepare   set session sql_mode='NO_ENGINE_SUBSTITUTION'
           77 Execute   set session sql_mode='NO_ENGINE_SUBSTITUTION'
           77 Close stmt    
           77 Prepare   select * from `users` where `id` = ? limit 1
           77 Execute   select * from `users` where `id` = 284 limit 1
           77 Close stmt    
           77 Quit
like image 27
M.S. Dousti Avatar answered Oct 12 '22 11:10

M.S. Dousti