//驗證聯合唯一索引(application_id,pfId,ad_type這三個字段在advertisements表裏的唯一索引)
$where = [
'application_id'=>$request->application_id,
'pfId'=>$request->pfId,
'ad_type'=>$request->ad_type
];
$request->validate([
"application_id" => [
"required",
Rule::unique('advertisements')
->where(function ($query) use ($where) {
return $query->where($where);
})
],
],['application_id.unique'=>'數據已存在']);
記得控制器上面要引入下面這兩個,並把Request依賴注入到方法裏
use Illuminate\Validation\Rule;
use Illuminate\Http\Request;
https://learnku.com/laravel/t/12988/how-do-i-write-the-unique-rule-of-the-union-unique-index