Saturday, March 11, 2017

[SOLVED]Laravel 5.4 how to handle query exception with custom eception

when ever i try to delete an artist with related song in child table. it returns this error.
QueryException in Connection.php line 647:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`laravel`.`artist_song`, CONSTRAINT `artist_song_artist_id_foreign` FOREIGN KEY (`artist_id`) REFERENCES `artists` (`id`)) (SQL: delete from `artists` where `id` = 1)

this is what i want. to protect the parent table, but what i want is to make the end user see a message saying like this "yOU CAN NOT DELETE AN ARTIST WITH RELATED SONGS PLEASE DELETE ALL SONGS OF THIS ARTIST FIRST'. so how can i catch this with custom exception.


via Mahamoud Mahamed

Advertisement