Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have

User table like

+==============+
|     User     |
+==============+
|      id      |
+--------------+
|   firstname  |
+--------------+
|    lastname  |
+--------------+
|     email    |
+--------------+
|   password   |
+--------------+

and my roles table

+==============+
|     Roles    |
+==============+
|      id      |
+--------------+
|     name     |
+--------------+

and my role_user table is

+=============+
|  role_user  |
+=============+
|   user_id   |
+-------------+
|   role_id   |
+-------------+

How can I check current logged user is admin or normal user?

like image 258
Hoque MD Zahidul Avatar asked Jan 08 '16 15:01

Hoque MD Zahidul


People also ask

How do I check permissions in Laravel blade?

Instead, use Laravel's native @can directive to check if a user has a certain permission. You can use @can , @cannot , @canany , and @guest to test for permission-related access.

How do you check if a variable is set or not in Laravel blade?

You can use the @isset blade directive to check whether the variable is set or not. Alternatively, if you have the default value for that variable you can directly use it as {{ $vatiable ?? 'default_value' }} .

What is the function of blade in Laravel?

In addition to template inheritance and displaying data, Blade also provides convenient shortcuts for common PHP control structures, such as conditional statements and loops. These shortcuts provide a very clean, terse way of working with PHP control structures while also remaining familiar to their PHP counterparts.


1 Answers

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.

like image 95
Haider Lasani Avatar answered Oct 24 '22 01:10

Haider Lasani