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?
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.
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' }} .
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With