I have an application on Laravel, I use the pgsql database. Let there is a model "Photo", it is stored in the corresponding database table. It uses other parts of the application through the foreign key , for example, it can be displayed as a user's avatar or used in the article.
My goal is as follows: when I try to delete a photo, I want to give something like this: "You cannot delete a photo, as it is used: 1) As a profile photo for such and such a user; 2) In an article such and such . That is, I need to find all the uses of a particular object in other tables.
Obviously, I can just sort through all possible places of use, but I don’t really like this solution. The second option is to parse the details of the exception, which the database throws when trying to delete an object with links ... But the format of the exception may change and then my script will stop working.
Is there some correct and elegant method to do this?