Wrap your code blocks in three back ticks. ` to make the code readable.
public function readable()
{
//Example
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a query for applying filters and search schools from table. I am getting all the variables through ajax request in laravel controller. I am using 'when' keyword for checking if that variable needs to be added in filter. Here is the query
$schools = School::where('is_active',1) ->whereBetween('tuition',[$min_tuition_fee,$max_tuition_fee]) ->whereBetween('application_fee',[$min_application_fee,$max_application_fee]) ->when($nationality_id,$education_country_id,$education_level_id,$grading_scheme_id,$exam_type_id,$grading_scale,$grading_avg,function ($q1) use($nationality_id,$education_country_id,$education_level_id,$grading_scheme_id,$exam_type_id,$grading_scale,$grading_avg){ $q1->whereHas('eligibility',function ($q2) use($nationality_id,$education_country_id,$education_level_id,$grading_scheme_id,$exam_type_id,$grading_scale,$grading_avg){ $q2->when($nationality_id,function ($q3) use($nationality_id){ $q3->where('nationality_id',$nationality_id); })->when($education_country_id,function ($q3) use($education_country_id){ $q3->where('education_country_id',$education_country_id); })->when($education_level_id,function ($q3) use($education_level_id){ $q3->where('education_level_id',$education_level_id); })->when($grading_scheme_id,function ($q3) use($grading_scheme_id){ $q3->where('grading_scheme_id',$grading_scheme_id); })->when($exam_type_id,function ($q3) use($exam_type_id){ $q3->where('exam_type_id',$exam_type_id); })->when($grading_scale,function ($q3) use($grading_scale){ $q3->where('grading_scale',$grading_scale); })->when($grading_avg,function ($q3) use($grading_avg){ $q3->where('grading_avg',$grading_avg); }); }); }) ->when($country_ids,function ($q1) use($country_ids){ $q1->whereHas('country',function ($q2) use ($country_ids){ $q2->whereIn('id',$country_ids); }); }) ->when($state_ids,function ($q1) use($state_ids){ $q1->whereHas('state',function ($q2) use ($state_ids){ $q2->whereIn('id',$state_ids); }); }) ->when($city_ids,function ($q1) use($city_ids){ $q1->whereHas('city',function ($q2) use ($city_ids){ $q2->whereIn('id',$city_ids); }); }) ->when($school_type_ids,function ($q1) use($school_type_ids,$school_ids){ $q1->whereHas('type',function ($q2) use($school_type_ids){ $q2->whereIn('id',$school_type_ids); }); }) ->when($school_ids,function ($q1) use($school_ids){ $q1->whereIn('id',$school_ids); }) ->when($discipline_ids,function ($q1) use($discipline_ids){ $q1->whereHas('disciplines',function ($q2) use($discipline_ids){ $q2->whereIn('id',$discipline_ids); }); }) ->latest() ->orderBy('title') ->paginate(10);
This query is working perfectly. I have used 'when' multiple times does it effect performance or ist is the right way to do it!
Please or to participate in this conversation.