My query is about how to deal with this kind of errors.
So far I know that this happens because you cannot delete a record that depends on another record from another table, which are related.
But my question is whether another kind of error, some message, or a different window than the common one can be shown when this kind of error appears.
I was thinking of doing it with Session::flash()
inside destroy()
my controller function, where it shows an error message, instead of going to that window. But I don't know how to do it very well. Thanks.
Additional Information:
In this way I have made the code where I try to set an error message.
public function destroy($id){
try {
$autos = AutosNew::find($id);
$autos->delete();
Session::flash('message', 'Automóvil eliminado correctamente del registro');
return redirect()->back();
} catch (Exception $e) {
return "Fatal error -" .$e->getMessage();
}
}
And if it fails it destroy()
would like to show an error message, instead of the error that the image I posted shows.
The problem is that you're not using try...catch correctly, I think your code should look something like this:
Even though your code is in the "try" block, when the error occurs trying to delete the record, the immediately following stops executing and the cursor jumps directly to the "catch" block; it is precisely there where you must decide what you will do with the error.