M
M
Michael742016-08-05 11:44:43
Laravel
Michael74, 2016-08-05 11:44:43

How to validate for the uniqueness of two fields in a bundle in Laravel 5?

How to create a rule so that the fields tk_id and num_id do not repeat in a bundle, for example:
id: 1, tk_id: 1 and num_id: 1 - true
id: 2, tk_id: 2 and num_id: 1 - true
id: 3, tk_id: 1 and num_id:1 - false

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Osher, 2016-08-05
@miraage

Learn to use a search engine.
https://github.com/felixkiss/uniquewith-validator

A
Alexander, 2018-12-06
@alexTitakoff

https://laravel.com/docs/5.3/validation#rule-unique

use Illuminate\Validation\Rule;

 public function rules()
    {
        $rules = [          
            'name_eng'=>'required|string',
            'field1' => [
                'required',
                Rule::unique('table_name')->where(function ($query) {
                    $query->where('field2', $this->get('field2'));
                })
            ],  
        ];

        return $rules;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question