如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
缓存优化 数据库查询缓存优化 数据库字段 show COLUMNS from 'user' //查询数据表的字段 生成数据表的缓存文件 php think optimize:schema //生成的目录在runtime/schema 路由 php think optimize:route //生成的目录在runtime/route.php 权限管理 权限粒度 用户-角色-权限 model.php namespace App; use Illuminate\Database\Eloquent\Model as EloquentModel; class Model extends EloquentModel { protected $guarded = []; } model中修改表头 protected $table = 'user'; 模型关联 adminuser 用户有哪些角色(多对多) public function roles(){ return $this->belongsToMany(\App\AdminRole::class,'admin_role_user','user_id','role_id')->withPivot(['user_id','role_id']); //关系表中的字段withPivot } 判断是否有某个角色 public function isInRole($roles){ //交集对比intersect //!! 返回bool类型 return !!$role->intersect($this->roles)->count(); } 给用户分配角色 public function assignRole($role){ //根据belongsToMany关联关系,直接确定当前的user_id return $this->roles()->save($role); } 取消用户分配的角色 public function deleteRole($role){ //detach 删除关系 return $this->roles()->detach($role); } 判断用户是否有权限(判断与权限的对象是否有交集) public function hasPermission($permission){ return $this->isInRoles($permission->roles); } AdminRole 获取当前角色的所有权限(多对多) public function permissions(){ return $this->belongsToMany(\App\AdminPermission::class.'admin_permission_role','role_id','permission_id')->withPrvot(['permission_id','role_id']); } 赋予角色权限 public function grantPermission($permission){ return $this->permissions()->save($permission) } 赋取消角色权限 public function grantPermission($permission){ return $this->permissions()->detach($permission) } 判断角色是否有权限 public function hasPermission($permission){ return $this->permissions->contains($permission); } adminPermission 权限 权限属于哪个角色 public function roles(){ return $this->belongsToMany(\App\AdminRole::class,'admin_permission_role','permission_id','role_id')->withPivot(['permission_id','role_id']); } compact() //创建一个包含变量名和它们的值的数组 view gate()实现权限管理 注册 //位置\app\providers\AuthServiceProvider.php public function boot() { $this->registerPolicies(); //拿出所有 $permissions = \App\AdminPermission::with('roles')->get(); foreach ($permissions as $permission) { <!-- 定义gate --> Gate::define($permission->name, function($user) use($permission) { <!-- 判断用户是否有该permission --> return $user->hasPermission($permission); }); } } 使用 路由方法: Routte::group(['middleware'=>'can:system'],function(){ ... }) 模板方法: @can('system') <!-- 有权限才显示 --> @endcan 控制器方法 $this->authorize('update',$post); Resource 资源路由 Route::resource('photo','\App\Admin\Controllers\PhotoController'); get /photos index photos/index get /photos/create create photos/create post /photos store photos/store get /photos/{photo} show photos/show get /photos/{photo}/edit edit photos/edit put/patch /photos/{photo} update photos/update delete /photos/{photo} destory photos/destory 只使用部分路由 Route::resource('photo','\App\Admin\Controllers\PhotoController',['only'=>['index','create','store']]); 系统通知notice laravel 的队列 驱动 修改驱动为database //config/queue.php 'default' => env('QUEUE_DRIVER', 'database'), 创建database对应的表(jobs) php artisan queue:table 执行 php artisan migrate 定义任务 创建任务 php artisan make:job SendMessage \app\Jobs\SendMessage.php public function __construct(\App\Notice $notice) { $this->notice = $notice; } public function handle() { // 通知每个用户 $users = \App\User::all(); foreach ($users as $user) { $user->addNotice($this->notice); } } NoticeController.php namespace App\Admin\Controllers; use App\Topic; use Illuminate\Http\Request; class NoticeController extends Controller { public function index() { $notices = \App\Notice::all(); return view('admin/notice/index', compact('notices')); } public function create() { return view('admin/notice/create'); } public function store(Request $request) { $this->validate($request, [ 'title' => 'required|min:3', 'content' => 'required|min:3' ]); $notice = \App\Notice::create(request(['title', 'content'])); dispatch(new \App\Jobs\SendMessage($notice)); return redirect('/admin/notices'); } public function show() { } public function edit() { // } public function update(Request $request, Topic $topic) { // } public function destroy() { } } 启动队列 php artisan queue:work //短期使用 nohup php artisan queue:work >> /dev/null & //长期使用 预加载 with load demo 1. $post = Post::with("user")->paginate(6); 2. $post = Post::paginate(6); $post->load('user'); {{$post->user->id}} 不加入预加载,在页面中会执行一次sql