在我的用户表中,我有一个角色列,其值为Teacher和Student。如果是用户角色老师,我想插入数据。否则给一条消息"仅限老师"。 这是我的控制器:
$validator = Validator::make($request->all(), [
'title' => 'required|exists:users,role==[Teacher]',
]);
if ($validator->fails()) {
Flash::success('Only for Teacher');
return redirect(route('works',['class_id'=>$class_id]));
}else{
$works = new assainments();
$works -> title = $text;
//$works -> file = $fileName;
$works -> class_id = $class_id;
$works -> users_id = Auth::user()->id;
$works -> save();
Flash::success('Your works has been posted');
return redirect(route('works',['class_id'=>$class_id]));
但它没有用。它只显示错误消息"仅适用于教师"
答案 0 :(得分:-1)
exists
验证器规则适用于表,列和额外条件。查看更多定义:
https://laravel.com/docs/5.2/validation#rule-exists
但你可以通过检查if条件来解决这个问题:
if( \Auth::user()->role == 'Student' )
{
Flash::success('Only for Teacher');
return redirect(route('works',['class_id'=>$class_id]));
}