我想一起验证两列具有唯一性

时间:2019-08-21 06:35:00

标签: laravel laravel-5 laravel-validation

验证Laravel中两列的唯一性。

控制器

function insert(Request $request)
    {       
    $this->validate($request, [
     'School_name' =>  'required',
     'class'       =>  'required',
     'Address'     =>  'required|composite_unique:School_name,Address',
     'Country'     =>  'required',
     'State'       =>  'required',
    ]);

2 个答案:

答案 0 :(得分:0)

您可以尝试此操作,假设您的表名是schools

function insert(Request $request)
{       
    $this->validate($request, [
        'School_name' =>  'required',
        'class'       =>  'required',
        'Address'     =>  'required|unique:schools,School_name,NULL,id,Address,'.$request->School_name,
        'Country'     =>  'required',
        'State'       =>  'required',
    ]);
}

答案 1 :(得分:0)

首先通过Composer或cmd安装软件包:

composer require felixkiss/uniquewith-validator

将以下内容添加到config / app.php中的providers数组中:

'providers' => [
// ...

Felixkiss\UniqueWithValidator\ServiceProvider::class,
],

然后在Controller中:

  function insert(Request $request)
            {       
                $this->validate($request, [
        'School_name'         =>  'required',
        'class'             =>  'required',
        'Address'            =>'required|unique_with:school,Address,School_name',
        'Country'             =>  'required',
        'State'               =>  'required',
                ]);