//验证联合唯一索引(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
本文介绍了一种在Laravel框架中使用验证联合唯一索引的方法,通过在一个表中定义多个字段的唯一组合来确保数据的唯一性。示例代码展示了如何在控制器中使用Illuminate Validation Rule进行联合唯一性的校验。
542

被折叠的 条评论
为什么被折叠?



