Laravel 4 - how to use a unique validation rule / unique columns with soft deletes?

前端 未结 5 1313
盖世英雄少女心
盖世英雄少女心 2020-12-29 11:24

Assume, you are trying to create a new user, with a User model ( using soft deletes ) having a unique rule for it\'s email address, but there exists a trashed user within th

5条回答
  •  旧巷少年郎
    2020-12-29 12:20

    Laravel offers "Additional Where Clauses".

    My url validation rule (from the update model method) looks like this:

    $rules['url'] = 'required|unique:pages,url,'.$page->id.',id,deleted_at,NULL';
    

    This means that the url must be unique, must ignore the current page and ignore the pages where deleted_at id not NULL.

    Hope this helps.

提交回复
热议问题