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

arifshah180's avatar

put an if condition around where clause in laravel query

Hi,

Laravel query, we have a where clause.

->where('addresses.city_id',$city_id)

I want to apply this where clause if the city_id > 0. If the city_id =0, then I would not like to pass this where clause to the query. Can I do something like

$query = DB::table('users')->select('first_name') If($city_id > 0) { ->where('addresses.city_id',$city_id) } ->get();

If not, where would be the other possible options?

0 likes
1 reply

Please or to participate in this conversation.