When i using request it work correctly
$query->when($request->has('flag_id'), function ($q) {
return $q->where('flag_id', request('flag_id'));
});
but when i using $flag_id param it's still same result
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
hello , i have search pipe here like
public function search(Request $request)
{
$data = $request->all();
$request->request->add(['filter' => 'advanced']);
$title = "clients";
$query = Client::query();
$client_types = ClientType::whereStatus(true)->get();
$labels = Label::all();
$flags = Flag::all();
$query->when($request->has('flag_id'), function ($q, $flag_id) {
return $q->where('flag_id', $flag_id);
});
$clients = $query->get();
return view('backend.clients', compact('title', 'clients', 'client_types', 'labels', 'flags'));
}
and i get the same result every time i search so when i do dd() i found is the flag_id every time the same , but when i do dd() for the $request->input('flag_id') i found it changed and work correctly so i need to pass the $request data to when method , i found reqeust() function but i don't know if secure or not
The $request variable isn't accessible inside the closure You'll have to add use ($request) after the function() and before the brackets {}
It will look something like this
$query->when($request->has('flag_id'), function ($q) use ($request) {
return $q->where('flag_id', $request->flag_id);
});
Please or to participate in this conversation.