Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

motinska94's avatar

Is there a way to add a where clause the query everytime it's called?

I have city_id in my users table. And every user has more than one product. Is there a way to automatically add whereCityId(Auth::user()->city_id) to all my queries?

I know I can make a hasone method in the model but I want it to be included as soon as I make a query for it.

In simpler words, I want to get Model::whereCityId(Auth::user()->city_id)->get() when I type Model::all()

0 likes
2 replies
motinska94's avatar

@Sinnbeck I'm pretty new to laravel and I have no idea how this works but I feel like this is exactly what I need. Thanks! I'll look it up.

Edit : Yup, it did! Took me 3 minutes to learn and implement. Thanks again!

Please or to participate in this conversation.