mardi 28 février 2023

How to filter a Laravel parent model and all its nested child models in one place based on user info?

Imagine I have these Models : User, Country, State, City, Library, and we know that they have nested relationships together. Country Model has many nested child Models that belong to it. I need to filter all the data from all the child Models of Country based on User's country of origin.

For example I have a User from US and I want to limit his access to only view and edit the information related to the US. Like in LibraryController I need to get all the libraries that belong to US.

I can implement the logic in every Model by filtering the data using whereHas ( and filter the result based on country relationship ) but I want to know is there a way to do this only in the Parent ( Country ) Model? This way a lot of redundant code will be avoided.

Aucun commentaire:

Enregistrer un commentaire