如何验证laravel 5.2中的列

时间:2016-09-20 22:09:39

标签: laravel-5.2

在我的用户表中,我有一个角色列,其值为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]));

但它没有用。它只显示错误消息"仅适用于教师"

1 个答案:

答案 0 :(得分:-1)

laravel 5.2

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]));
}