0
votes

I want to ignore the slug if value is not changed. I am currently getting this error whenever i update the form.

This is my request on validation.

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;

class ProductInsertFormRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; }

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'title'=>'required',
        'regularPrice'=>'required',
        'slug'=>'required|alpha_dash|min:5|max:255|unique:products,slug',
    ];
}

}

2
You are getting error on updating. Show the code of updating post.Ashutosh Sharma

2 Answers

0
votes

to ignore a particular row in database table when checking unique, you need to use unique() rule with ignore() function

'slug' => ['required', 'alpha_dash', 'min:5', 'max:255', Rule::unique('products', 'slug')->ignore($this->product->slug)

This will check unique in 'slug' column of the 'products' table, except this particular row which has 'this slug'

0
votes
  1. Ignore() function in Rule is working only with id.
  2. So I changed the code into following:

    public function rules()
    {
    $decrypted = Crypt::decrypt($this->id);
    //$slug = Product::whereId($decrypted)->pluck('slug')->first();
    
    //return dd($slug);
     return [
        'title'=>'required',
        'regularPrice'=>'required',
        'slug' => ['required', 'alpha_dash', 'min:5', 'max:255', Rule::unique('products', 'slug')->ignore($decrypted)]
    ];
     }