How to validate a database column on uniqueness in Laravel

Categorized as Laravel Tagged

You can see if the column in your Laravel application DB is unique or not. For example, the article table has slug and author_id with the unique slug for each author. See the code below, it’s useful for multi-tenancy:

public function rules()
{
  return[
    'slug' => [
      Rule::unique('articles')->where('author_id', Auth:id())
    ],
  ];
}

Leave a reply

Your email address will not be published.