dongmaxi6763 2019-03-21 10:06
浏览 143

如何根据其他字段规则对唯一数组进行自定义验证规则

I have working on Laravel 5.6. I have 4 dependent unique columns but don't know how to validate these dependent columns here is my migration for unique constrain:

$table->unique(array('lvl4','document_type','nature_id','type_id'),'u_coa_lvl4_asn_dnt_uk');

lvl4 is array.

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dos71253 2019-03-22 05:19
    关注

    You can try manaully validating with something like this:

    $data = [
        'lvl4' => 'Level 4',
        'document_type' => 'Some type',
        'nature_id' => 1,
        'type_id' => 7,
    ];
    
    $validator = Validator::make($data, [
        'data.lvl4' => [
            'required',
            Rule::unique('your_table')->where(function ($query) use($data) {
                return $query->where('lvl4', $data['lvl4'])
                    ->where('document_type', $data['document_type'])
                    ->where('nature_id', $data['nature_id'])
                    ->where('type_id', $data['type_id']);
            }),
        ],
    ]);
    
    if($validator->fails()) {
        // it fails validation ...
    }
    

    This is untested, but something like this should work for you.

    https://laravel.com/docs/5.6/validation#rule-unique

    评论

报告相同问题?