Laravel 5 Validation unique pass variable placeholder

前端 未结 4 1846
时光说笑
时光说笑 2021-02-09 20:42

Is it possible to pass the unique validation method extra where clauses with a variable?

Heres an example:

In my model I have my validation rules.



        
4条回答
  •  太阳男子
    2021-02-09 21:13

    The Laravel documentation on validation has a sub-section titled, “Adding Additional Where Clauses” in the unique rule section:

    You may also specify more conditions that will be added as "where" clauses to the query:

    'email' => 'unique:users,email_address,NULL,id,account_id,1'

    In the rule above, only rows with an account_id of 1 would be included in the unique check.

    Unfortunately you will have to concatenate a variable value if you want it passed as part of the conditions:

    'circle_id' => 'required|unique:donations,circle_id,NULL,id,cause_id,'.$causeId,
    

    Or you make it more readable by specifying validation rules as an array:

    'circle_id' => [
        'required',
        'unique:donations,circle_id,NULL,id,cause_id,'.$causeId,
    ],
    

提交回复
热议问题