验证行中存在两列Laravel

时间:2018-07-26 07:35:25

标签: laravel-5.4

如何通过#Laravel 5.4验证连续存在的两列 现在按以下方式进行操作:

我的控制器代码:

$validator = \Validator::make($request->all(), [
    'classlevel_id'=>'required',
    'book_name'=>'required|string',
    'class_no'=>'required|integer',
]);

if($validator->fails())
{
    return response()->json(['errors'=>$validator->errors()->all()]);
}else{
    $valid = DB::table('books')->where('class_no',$request->class_no)->where('book_name',$request->book_name)->exists();
    if($valid){
        return response()->json(['warning'=>"Book : ".$request->book_name." and class : ".$request->class_no." already Exists"]);
    }
}

我还尝试按照以下方式进行验证:

$validator = \Validator::make($request->all(), [
    'classlevel_id'=>'required',
    'book_name'=>'required|string|unique:books|required_with:class_no',
    'class_no'=>'required|string|unique:books|required_with:book_name',
]);

是否有任何Laravel Validate命令用于验证连续存在的两列?

1 个答案:

答案 0 :(得分:0)

您必须使用Laravel自定义验证规则 https://laravel.com/docs/5.6/validation#using-rule-objects