问题描述
我已经创建了具有用户角色和权限的项目
i have created the project with user roles and permissions
这是我的表格和模型
users
-应用程序用户列表-型号名称[User
],
users
--list of the application users --Model Name [User
],
roles
-应用程序内部可用的角色列表-型号名称[Role
],
roles
--list of the roles available inside the application --Model Name [Role
],
permissions
-应用程序内部可用的Permisisons列表-型号名称[Permisions
],
permissions
--list of the Permisisons available inside the application --Model Name [Permisions
],
这是我的关系表
role_user
保存roles
表和users
表之间的关系
role_user
Which hold the relationship between the roles
table and users
table
permission_role
保留permissions
表和roles
表之间的关系
permission_role
Which hold the relationship between the permissions
table and roles
table
permission_user
保留permissions
表和users
表之间的关系
permission_user
Which hold the relationship between the permissions
table and users
table
我的模型内关系代码
User.php模型
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模型
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模型
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
*/
最后我创建了名为
PermissionServiceProvider
并且在serviceprovider的启动方法内部,我添加了代码
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
是否可以缓存所有权限和角色以登录用户
Is there any way to cache all the permisisons and roles to logged in user
已编辑
根据一些建议,我尝试了laravel缓存程序包
As per Some Suggestions i have tried laravel cache package
它对我不起作用
推荐答案
您正在寻找laravel模型缓存包 https://github.com/GeneaLabs/laravel-model-caching .
You are looking for laravel model caching pakage https://github.com/GeneaLabs/laravel-model-caching.
我建议安装带有Redis的软件包.在处理队列时,它也非常有用.
I recommend to install package with redis. Also it is very useful when working with queues.
按预期工作.我的项目的屏幕截图.
Works as expected. Screenshots from my project.
之前:
之后:
这篇关于Laravel缓存数据库查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!