Laravel6驗證器驗證多個字段的唯一索引

  //驗證聯合唯一索引(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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章