function ($query) use ($request) { if ($request->has_ongoing_order) { $query->has("onGoingOrder"); } $query->with(["onGoingOrder" => function ($query) { $query->with(["paramedic" => function ($query) { $query->select("id", "name"); }])->select("id", "bed_id", "paramedic_id", "status"); }]); }])->with("building")->where("project_id", $request->project_id)->orderBy("building_id")->orderBy("myindex")->get(); return response()->json($areas->toArray()); } }