Validating a Unique Slug on Update in Laravel 5

laravel, laravel-5, laravel-request, laravel-validation, php

Solution

Try to modify your rule like following(in form request class):

public function rules()
{
    return [
      'name'  => 'required,min:3',
      'slug'  => 'required|alpha_dash|unique:categories,slug,'.$this->id')
    ];
}

It works for me.

Problem

I currently have a model that has a text field and a slug field. I validate that the slug is unique in my form request class: ``` public function rules() { return [ 'name' => 'required|min:3', 'slug' => 'required|alpha_dash|unique:questions' ]; } ``` This works fine on create and properly denies the creation of duplicate slugs. However on my update method, it won't let me save a record because the slug already exists. Of course the slug does exist, but it exists on the record being edited, so I would like to continue to allow it to be saved. However, it should not be able to be changed to a slug on ANOTHER record. Here's what my update ArticlesController method looks like: ``` public function update(Article $article, ArticleRequest $request) { $article->update($request->all()); return redirect('articles'); } ``` Is there a way to make this work in L5?

Original source

Related problems