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

MahmoudAdelAli's avatar

Get request data inside query using when

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

0 likes
8 replies
MahmoudAdelAli's avatar

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

derba's avatar
derba
Best Answer
Level 1

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);
});
MahmoudAdelAli's avatar

@derbax i do it like that , but i don't know if this a secure or not

  $query->when(request('lastname'), function ($q, $lastname) {
      return $q->where('lastname', 'LIKE', "%${lastname}%");
    });

derba's avatar

@MahmoudAdelAli

Laravel ORM uses PDO parameter binding by default which separates the query of its parameters in this cause the last name expression

It will look something like this"select * from `clients` where `lastname` Like ?" and sends the value as a query binding

The ? will be replaced with the string provided

This approach prevents SQL injections

1 like
MahmoudAdelAli's avatar

@derba thank you , i have a little question when i use

 ->when(request('created_at_from') ?? null, function ($q, $create_at_from) {
        $q->where('created_at', '>=', $create_at_from);
      })

every thing is okay , but what if i need to search inside relation i mean i have clients and client_details and inside client_details there's nationality_number how i can search inside it ?

derba's avatar

@MahmoudAdelAli

You would use whereHas

$query->whereHas('clientDetails', function($query) use ($value) {
	$query->where('nationality_number', $value);
});

note: use the relation name defined in the Client model in this case it I supposed it is called clientDetails

1 like

Please or to participate in this conversation.