C
C
chelkaz2016-10-14 07:30:54
Laravel
chelkaz, 2016-10-14 07:30:54

How to check for uniqueness when updating a table?

When creating a table, checking for the uniqueness of the desired columns is clear. But how to logically check on update? For example, there are fields:
--------------------
name: Name
sort: 500
section: 12
------------------ - And in the sections
table , the name field must be unique. When updating, for example, I only change the sort field. And now, when checking for uniqueness, there will be an error, since the name field already exists in itself ... I check through validation:

$validator = Validator::make($request->all(), [
            'sect_name' => 'required|unique:sections,name|max:255',
        ]);

Is it possible to check with everyone but yourself?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor, 2016-10-14
@chelkaz

unique has many parameters, including the id of the record to be ignored during validation, check the documentation for the order of the parameters.
In 5.2, this is the third parameter.
The following syntax is available in the latest 5.3.*:
Documentation: https://laravel.com/docs/5.3/validation#rule-unique
Section Forcing A Unique Rule To Ignore A Given ID

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question