Home > Blockchain >  Filament SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
Filament SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

Time:09-11

When I create a new row in the table 'partidos' I get this message:

'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '

That is ok, I know it is a duplicate entry, but I get an error page from Laravel. My question is, how I can get an alert or similar instead of that error page?

I tried to use laravel validation rules, but I don't know how to use them with Filament

enter image description here

Thanks

CodePudding user response:

A QueryException is being thrown due to the duplicate key violation in your partidos table.

You could encapsulate your statement(s) in a try/catch block to catch and handle the exception however you see fit. For example:

try {
    // perform your database action here
} catch(\Illuminate\Database\QueryException $ex){ 
    // $ex->getMessage(); will provide a string representation of the error
    // from here you can handle the exception and return a response
}

Alternatively, you can use validation, specifically the unique rule to validate any values that must be unique in the database table are in fact unique.

public function action(Request $request)
{
    // if validation fails, laravel will redirect back to the page with errors
    $request->validate([
        'field' => ['required', 'unique:partidos'],
    ]);
}
  • Related