0

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

Peppermintology
  • 9,343
  • 3
  • 27
  • 51
Juan Diaz
  • 3
  • 1
  • you can use unique validation rule in your controller like 'email' => 'unique:users,email_address', //here users is table name and email_address is column name – Sanjog Karki Sep 10 '22 at 15:28

1 Answers1

0

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'],
    ]);
}
Peppermintology
  • 9,343
  • 3
  • 27
  • 51