2 回答

TA貢獻2080條經驗 獲得超4個贊
'branch_name' =>['required', 'unique:corp_branches,corp_id,'.$this->id.',NULL,corp_id,branch_name,'.$request->input('branch_name')]

TA貢獻1802條經驗 獲得超4個贊
您需要自定義驗證規(guī)則。
步驟 #1 - 生成規(guī)則
php artisan make:rule UniqueBranch
步驟 #2 - 修改app/Rules/UniqueBranch.php
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class UniqueBranch implements Rule
{
protected $corp_id;
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct($corp_id)
{
$this->corp_id = $corp_id;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
// Assume your model name is `Corporation`
$corporation = Corporation::find($this->corp_id);
if ($corporation) {
$exists = $corporation
->corp_branches() // assume you have relation to braches
->where('name', $value)
->count();
if (!$exists) {
return true;
}
}
return false;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The branch name has already been taken.';
}
}
步驟 #3 - 使用UniqueBranch
'branch_name' => [
'required',
'string',
'max:100',
new UniqueBranch($this->get('corp_id'))
]
- 2 回答
- 0 關注
- 102 瀏覽
添加回答
舉報