如何向函数 __construct() 添加一些条件,例如如果中间件是 admin 或当前用户 ID 等于此帖子 user_id,我有用户创建的帖子,因此唯一可以编辑的是创建此帖子的人或管理员我尝试了下面的代码,所以 role_id=4 表示这是管理员,
先感谢您
public function __construct($post_number)
{
$posts=Post::where('post_number',$post_number)->first();
where(Auth::User()->id==$posts->user_id)OR (Auth::User()->role_id==4);
}
您可以使用 laravel when 方法。现在你的代码应该是这样的。
public function __construct($post_number)
{
$posts=Post::where('post_number',$post_number)->first();
if($posts->user_id == auth()->user()->id || auth()->user()->role_id == 4) {
//cann edit the post
} else {
//cannot edit the post
return false;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句