Reputation: 11949
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?
Upvotes: 11
Views: 31792
Reputation: 111829
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
Upvotes: 26
Reputation: 789
the methods shared works. the problem is if you have to check more than once per page, it hits the database that many times. for instance, let's say you have a navigation with 8 links. the first, fourth, and seventh links should only be visible by admin only. that query will hit your database 3x. maybe i'm just anal but it's a duplicated request.
i'm trying to find another way to store a variable that loads once in the view/template so that every time i need to check if it's an admin, i check the variable and not hit the database every time. i've done it via controller -> view, but not just view alone in a template. i'm thinking of creating a helper method and returning an object to be checked once per page load.
Upvotes: 1
Reputation: 11949
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
Upvotes: 4
Reputation: 169
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.
Upvotes: 8
Reputation: 173
So you have some field isAdmin if it is 1 for example user is admin if not it is not. When user is loged check with (Auth::user()->isAdmin == 1)
then user is admin else it is not
with Auth::user()->
u can check any field from user table of current logged user.
Best Regards
Upvotes: -3