簡體   English   中英

如何根據其他字段規則對唯一數組進行自定義驗證規則

[英]How to make Custom Validation rule for unique Array dependent on other field laravel

我在Laravel 5.6上工作。 我有4個依賴的唯一列但不知道如何驗證這些依賴列這里是我的唯一約束遷移:

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

lvl4是數組。

提前致謝

您可以嘗試通過以下方式進行manaully驗證:

$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 ...
}

這是未經測試的,但這樣的事情應該對你有用。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM