How to check user is Admin or not in laravel blade view

You need to add roles relationship in your User model like so:

public function roles() 
{
   return $this->belongsToMany(App\Role::class);
}

and now you need to create isAdmin user like so:

public function isAdmin() 
{
   return in_array(1, $this->roles()->pluck('role_id')->all());
}

As 1 you put id of your admin role. Of course it could be also defined in other way, but everything depends on how this will be used.

It could be also defined this way:

public function isAdmin() 
{
   return $this->roles()->where('role_id', 1)->first();
}

and now in your Blade you can do:

@if (auth()->check())
   @if (auth()->user()->isAdmin())
      Hello Admin
   @else
      Hello standard user
   @endif
@endif

Role.php

use Illuminate\Database\Eloquent\Model;

class Role extends Model {

    protected $fillable = [
        'name'
    ];

    /**
     * A role can have many users.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function users() {

        return $this->belongsToMany('App\User');
    }

}

Then you can add this to User model:

public function isAdmin()
{
    foreach ($this->roles()->get() as $role)
    {
        if ($role->name == 'Admin')
        {
            return true;
        }
    }
}

View

@if(Auth::check())
    @if (Auth::user()->isAdmin())
        <h2>Admin user enter code here<h2>
    @endif
@endif

It's not an ACL for this simple functionality you don't even need a database table roles you can add extra tinyInteger status column and add numbers for example:

  • 0 = Disabled
  • 1 = Visitor
  • 2 = Admin.

To make it functional add following code to your User.php.

public function isDisabled ()
{
    return $this->statusCheck();
}

public function isVisitor ()
{
    return $this->statusCheck(1);
}

public function isAdmin ()
{
    return $this->statusCheck(2);
}

protected function statusCheck ($status = 0)
{
    return $this->status === $status ? true : false;
}

To check in blade template you can add

@if(Auth::user()->isDisabled())
    You are not Active
@elseif(Auth::user()->isVisitor())
    Welcome to example.com
@elseif(Auth::user()->isAdmin())
    Welcome Admin
@endif

Moreover you can make blade custom directives, paste this code to your app/providers/AppServiceProvider.php in boot() method.

// Blade custom directives for isAdmin

    Blade::directive('isAdmin', function() {
        return "<?php if(Auth::user()->isAdmin()): ?>";
    });

    Blade::directive('endisAdmin', function() {
        return "<?php endif; ?>";
    });

// Blade custom directives for isVisitor

    Blade::directive('isVisitor', function() {
        return "<?php if(Auth::user()->isVisitor()): ?>";
    });

    Blade::directive('endisVisitor', function() {
        return "<?php endif; ?>";
    });

// Blade custom directives for isDisabled

    Blade::directive('isDisabled', function() {
        return "<?php if(Auth::user()->isDisabled()): ?>";
    });

    Blade::directive('endisDisabled', function() {
        return "<?php endif; ?>";
    });

To call this you use need to write following lines in your blade view

@isAdmin()
     Welcome Admin
@endisAdmin

@isVisitor()
     Welcome to example.com
@endisVisitor

@isDisabled()
     Your are not active
@endisDisabled

In short laravel provides you a number of ways to solve a problem, it just depend on your need and application structure.