如何驗證Laravel 4中兩列的唯一性?如何驗證Laravel 4中兩列的唯一性?
假設,first_name
和last_name
是兩列。我想檢查first_name last_name
將是唯一的。
如何驗證Laravel 4中兩列的唯一性?如何驗證Laravel 4中兩列的唯一性?
假設,first_name
和last_name
是兩列。我想檢查first_name last_name
將是唯一的。
您可以編寫自定義Validatior,
http://laravel.com/docs/validation#custom-validation-rules
Validator::extend('foo', function($attribute, $value, $parameters)
{
/*
check whether "first_name + last_name" is unique with SQL. Pass them as parameters
*/
});
有開發包,一個社區:https://github.com/felixkiss/uniquewith-validator。
"felixkiss/uniquewith-validator": "dev-master"
。'Felixkiss\UniqueWithValidator\UniqueWithValidatorServiceProvider'
最後使用如下:
$rules = array(
'first_name' => 'unique_with:users,last_name',
);
檢查package page。
如果不使用此包「uniquewith-validator」,是否可以對多列應用獨特驗證?我只是希望有一種語法來爲模型中的唯一驗證編寫一個規則,如'label'=>'required | unique:sections,label,'。$ this-> event_id。',event_id,但它給我一個語法錯誤。我如何在此驗證中傳遞發佈的事件ID,以便它可以檢查數據庫中是否存在具有選定事件ID的標籤? – neeraj