How to prevent insert email if already exist with specific id in laravel?

前端 未结 2 615
天命终不由人
天命终不由人 2020-12-21 09:23

I have table in which i\'m trying to store email addresses. These email addresses will be save with user_id.

For example in email_list tabl

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-21 09:42

    Forcing A Unique Rule To Ignore A Given ID

    You can specify an ID to be ignored as the optional third parameter. Furthermore, if your table uses a primary key column name other than id, you may specify it as the optional fourth parameter

    'email' => "unique:{$table},{$field},{$user->id},{$idField}"
    

    So in your case, it'd be as follows

    'email' => "unique:email_list,email,{$user->id},user_id'
    

提交回复
热议问题