Is using @if(Auth::user()) the same as @auth in my blade view?

Viewed 54

I realized I am using 2 ways to prevent people who are not logged from seeing stuff that are meant for members only. The first one is using @if(Auth::user()) and the second one is using @auth. For example:

@auth
    <button class="submit-btn like liked" id='{{ $image->id }}'>
@endauth

And

@if(Auth::user())
    <button class="submit-btn like liked" id='{{ $image->id }}'>
@endif

Is there any difference between those 2 examples or should I just stick to one of them?

2 Answers

Since version 5.4.29 they are basically the same. The shorter @auth and @endauth are only a shorter/hidden way for the if-else construction. For your own comfort, it is a good idea to stick to one of them.

The real moment it shines is when you are using guards, so you don't have to do something like this (assuming you have a usertype 'administrator'):

@if(Auth::user()->type === 'administrator')

Instead, the following suffices:

@auth('administrator')

More correctly to use:

Auth:check() Auth:guest() in your code because they return bool

auth()->check() auth()->guest() (same with helper).

And @auth @guest in blade.

Related