Search code examples
phpvalidationlaravel-9

How can do multiple unique validation on Laravel 9?


How can we do multiple unique validation?

Let me explain my problem through an example. Let's say I have a table. otherusers table. I have Country ID Number, Email and Phone number in this table.

While the user is registering, if there is a input record that fits all three, I want it not to save. So let's say

11111111111, mymail@domain.com, 0555 555 555 55(5)

There is such a record in the table. If someone wants to register with the same information, i want they can not to register. I want it will say that there is already a record with this information.

But if one of these information is different, it will be able to register. for example

11111111111, mymail@domain.com, 0555 555 555 55(6)

If he tries to register in as a user, he will be able to register.

Of course, the table is not like this. I'm giving this example to explain the process I'm looking for.

Here my table settings

///...
Schema::create('otherusers', function (Blueprint $table) {
     $table->string("CountryIDNumber",10);
     $table->string("Email",100);
     $table->string("Phone",15);
     $table->unique(['CountryIDNumber', 'Email', 'Phone'],"my_uniques");
}
///...

and here my store function in controller.

    public function store(Request $request)
    {
        $request->validate([
            'CountryIDNumber' => 'required|max:10|unique:other_users',
            'Email' => 'required|max:100|unique:other_users',
            'Phone' => 'required|max:15|unique:other_users',
        ]);
        $data = $request->all();

        OtherUsers::Create($data);
        return redirect()->route('otherusers.show'); // stok listesi sayfası
    }

If I give it unique for each row as above it will check for each independently. However, I want to do validation in accordance with the index I have given here. If there is a line with the same three information entered from outside, the record should not be made.

And how can I show this error on the front side. I am using below codes to show validation errors

        @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
        @endif

The index I added to the table is working. When I try to make a recording I get the following error.

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '11111111111-mymail@domain.com-0555 555 555 555' for key 'my_uniques'

How can I do this what I want? I would appreciate your help.


Solution

  • I'm adding it as a new answer so that Christoph's comment remains in response.

    I changed my first answer with the codes below. (I deleted the selection query code and changed the controls from (If) to (Try Catch))

            try {
                 MyModel::Create($data);
                 return redirect()->route('show');
            } catch (\exception $th) {
                return redirect()->back()->withErrors(['This record has been insert before.']);
            }