基于文档 https://laravel.com/docs/5.5/authorization#via-middleware
use App\Post;
Route::put('/post/{post}', function (Post $post) {
// The current user may update the post...
})->middleware('can:update,post');
'can:update,post' 中的 'post' 是从 '{post}' 传递过来的变量。
我正在尝试使用 middleware('can:update,1')。它不工作。也许它搜索“$1”变量,如何将数字“1”传递给“can”中间件?
更新这是大门:
Gate::define('update', function ($user, $role){
$id = $user->id;
$roles = $user::find($id)->roles()->get();
$roleid = $roles[0]->pivot->role_id;
//above code just for get role id, and $role_id is 1
return $roleid === $role;
});
最佳答案
可能您没有为 Post 创建策略。
可以通过命令创建:
php artisan make:policy PostPolicy --model=Post
然后在该策略中实现方法更新。
关于php - 我如何在 Laravel 授权中将数字传递给中间件 "can",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46580169/