1

i have created the project with user roles and permissions

Here is my Tables and Model

users--list of the application users --Model Name [User],

roles--list of the roles available inside the application --Model Name [Role],

permissions--list of the Permisisons available inside the application --Model Name [Permisions],

Here is my relationship tables

role_user Which hold the relationship between the roles table and users table

permission_role Which hold the relationship between the permissions table and roles table

permission_user Which hold the relationship between the permissions table and users table

My Relationship Code inside Model

User.php Model

/** * Many-to-Many relations with Role. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function roles() { return $this->belongsToMany(Role::class); } /** * Many-to-Many relations with Permission. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function permissions() { return $this->belongsToMany(Permission::class); } public function hasPermission($permission) { return $this->hasPermissionThroughRole($permission) || (bool) $this->permissions->where('name',$permission->name)->count(); } public function hasPermissionThroughRole($permission) { foreach($permission->roles as $role) { if($this->roles->contains($role)) { return true; } } return false; } public function hasRoles($roles) { $roles = is_array($roles) ? $roles : func_get_args(); foreach ($roles as $role) { if ($this->hasRole($role)) { return true; } } return false; } /** * Returns if the given user has an specific role. * * @param string $role * * @return bool */ public function hasRole($role) { return $this->roles ->where('name', $role) ->first() != null; } 

Role.php Model

/** * Many-to-Many relations with Permissions. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function permissions() { return $this->belongsToMany(Permission::class); } /** * Many-to-Many relations with Users. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function users() { return $this->belongsToMany(User::class); } 

Permission.php Model

/** * Belongs-to-Many relations with Role. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function roles() { return $this->belongsToMany(Role::class); } /** * Belongs-to-Many relations with User. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function users() { return $this->belongsToMany(User::class); } /** * Belongs-to-Many relations with Modules. * * @return \Illuminate\Database\Eloquent\Relations\belongsToMany */ 

And then finally i have create the ServiceProvider named as

PermissionServiceProvider 

and Inside the boot method of the serviceprovider i have added the code

public function boot() { if (Schema::hasTable('permissions')) { Permission::get()->map(function ($permission) { Gate::define($permission->name, function ($user) use ($permission) { return $user->hasPermission($permission); }); }); } Blade::directive('role', function ($role) { return "<?php if(Auth::user()->hasRole({$role})): ?>"; }); Blade::directive('endrole', function ($role) { return "<?php endif; ?>"; }); } 

Every functions and relationship is working fine but the Queries are running every time i hit the refresh button Running Queries

Is there any way to cache all the permisisons and roles to logged in user

Edited

As per Some Suggestions i have tried laravel cache package

It is not working for me

1 Answer 1

3

You are looking for laravel model caching pakage https://github.com/GeneaLabs/laravel-model-caching.

I recommend to install package with redis. Also it is very useful when working with queues.

Works as expected. Screenshots from my project.

Before:

enter image description here

After:

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

is there any way to create our own model caching
Yes, laravel has built in tools laravel.com/docs/5.7/cache. But I think you will spend a lot of time to prepare it for all model methods and relations.
do you have any idea how to make the project with user roles and permisisons in laravel without any package
Nope, I didn't write my own cache system on laravel. But I suppose that you will have to write your own query builder for this. So, looks like it's not worth the gamble.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.