Which to use Auth::check() or Auth::user() - Laravel 5.1?

If I want to check whether the user is logged in within my Laravel 5.1 application I can either use

if (Auth::user()) {...}

or

if (Auth::check()) {...}

is there a reason to prefer one over the other when checking if a user is logged in?

No, the accepted answer is not correct.

Auth::check() defers to Auth::user(). It's been that way since as long as I can remember.

In other words, Auth::check() calls Auth::user(), gets the result from it, and then checks to see if the user exists. The main difference is that it checks if the user is null for you so that you get a boolean value.

This is the check function:

public function check()
{
    return ! is_null($this->user());
}

As you can see, it calls the user() method, checks if it's null, and then returns a boolean value.

auth middleware - Authentication - Laravel, Note: Authorization was added in Laravel 5.1.11, please refer to the upgrade guide If you would like to use the Gate facade to check if a user other than the  The code that Laravel uses to check if the user is logged when the frontend pings the backend is exactly the same as when the data is preloaded in to the view. So why the difference. Why does Auth:check or Auth:user log the user out when served together with the view?

If you just want to check if the user is logged in, Auth::check() is more correct.

Auth::user() will make a database call (and be slightly heavier) than Auth::check(), which should simply check the session.

Authentication routes - Authentication - Laravel, You may use the after method to define a callback to be executed after all other authorization checks: Gate::after(function ($user, $ability, $result, $arguments) { if   If your application is not using Eloquent, you may use the database authentication driver which uses the Laravel query builder. When building the database schema for the App\User model, make sure the password column is at least 60 characters in length.

Auth::guard('admin')->user()->email 

try this it worked for me.

authentication guard - Authentication - Laravel, Determining If A User Is Authenticated. To determine if the user is already logged into your application, you may use the check method: if (Auth::check()) { // The  If you want something to execute you should use laravel 5 middleware. 1.- php artisan make:middleware newMiddleware. 2.- Add your middleware to all routes you need by setting the middleware property.

I recomend you to define $user = auth()->user(); If you want to check if user is authenticated or not and user model properties like email, name, ... in your code.

Because auth()->user() and auth()->check() both will query to database. If you want more speed in your apps, define $user = auth()->user(); then you can us it in your codes and also you can check if user is authenticated via $user == null;.

Don't use auth()->check() and auth()->user() both in one function or one block of code to have low queries to database and more speed apps! ; )

Authorization - Laravel, Auth::user() — You can check if a user is authenticated or not via this method from the Auth Facade. It returns  I found a simple solution for this. Artisan create App\Http\Controllers\Auth\LoginController, in this default controller just add this code if you have some conditions to login, for example I have a field state, you posibbly have status, email_status or other.

Authentication - Security - Laravel, I use other libraries that uses laravel's Auth::user() and Auth::check() to retrieve Ideally I would like Auth::user() to return Sentinel::getUser() or some other function Thanks for the reply @martinbean but is this possible to do in laravel 5.1? Hi im storing data in database but when i use Auth::user()->name it is not working. in my other controller its working. But when i hard coded the name and the id it works.

How to Create Your First Laravel App - Authentication Tutorial, Get code examples like "laravel check if authenticated" instantly right from your show(User $user) laravel · laravel get auth user · laravel 7 Auth::routes() just  Laravel framework- Why aren't I able to use Auth::user(), (to see if user has been logged in) in the controller of the laravel project. Is the Session not connected to the controller? HomeController.php

How can I extend Laravel's Auth?, This is about Laravel 5.3.6. I am able to login successfully and I can check Auth User after login. I can show the exact location where Auth::guard() has current user object. Below are the details. Go to Login Controller; Go to AuthenticatesUsers Trait; Go to sendLoginResponse method. User reaches here successfully because user is authenticated

Comments
  • Please take a look at my answer. :)
  • Honestly, I'm not sure. :) But for me, it's just the intent behind the methods. I like writing Auth::check() if I want to "check" if a user is logged in. I like writing Auth::guest() to check if a user is a guest. It's a bit more readable to me than doing !Auth::check(). I like writing Auth::user() if I want to get the currently logged in user. You can probably get away with Auth::user() in this case because PHP is a dynamic language, but I think it helps to be a bit more explicit.
  • It's so easy to write queries in laravel we often don't realize all the queries we write. It's much better to define this variable and then use the variable. I've seen pages that have 7-8 auth()->check()'s and in the beginning we didn't realized we were hitting the database every time!