AH.Pooladvand
AH.Pooladvand

Reputation: 2059

Laravel unique validation

How can I perform this migration rule to laravel validation rule

$table->unique(['field_1', 'field_2']);

Lets say that i have a categories table with title, slug and category_type fields.

So what I want is something like this:

Slug             Category_type
foo              Blog

foo              News           => Pass the validation
foo              Blog           => Return back with error...

so far I tried this but I still got no result?

$rules = [
            'slug' => 'required|max:70|unique:categories,slug,null,id,category_type,' . Blog::class . '|regex:/(^[A-Za-z-_ ]+$)+/',
            'title' => 'required|max:70',
        ];

Upvotes: 0

Views: 208

Answers (1)

Jithin Jose
Jithin Jose

Reputation: 1821

The format you have given is correct,

unique:categories,field_1,null,id,field_2,field_2_value

ref, https://laravel.com/docs/5.1/validation#rule-unique

But remember Blog::class returns full class path.

Upvotes: 1

Related Questions