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

shaddark's avatar

Laravel 9 -query filter data and ignore where condition if null

Hello, i'm trying to filter the results of a table. My form in index.blade

        <form action="" method="GET">
          @csrf
          @method('GET')
          <input class="" type="text" name="order_id" id="order_id" value="" placeholder="ID">
          <input class="" type="text" name="client_id" id="client_id" value="" placeholder="Codice">
          <input class="" type="date" name="date_order" id="date_order" value="">
          <input class="" type="date" name="date_request" id="date_request" value="" placeholder="Codice">
          <select class="" name="status" id="status">
            <option value="" disabled selected>Stato</option>
            <option value="0">Da Confermare</option>
            <option value="1">Confermato</option>
            <option value="2">In Elaborazione</option>
            <option value="3">Elaborato</option>
          </select>
          <button class=""><i class="fa-solid fa-magnifying-glass"></i></button>
        </form>

In my controller :

        $filters = [
            'filter_by_id' => $request->input('order_id'),
            'filter_by_client' => $request->input('client_id'),
            'filter_by_date_order' => $request->input('date_order'),
            'filter_by_date_request' => $request->input('date_request'),
            'filter_by_date_status' => $request->input('status'),
        ];
       // $filtered = array_filter($filters);
       dump($filters);
        if (!empty($filters)) {
            $data_grid = OrderGrid::where('tipologia', 'O')
                                    ->where('user_id', Auth::id())
                                    ->where('id', $filters['filter_by_id'])
                                    ->where('codice_cliente', $filters['filter_by_client'])
                                    ->where('data_ordine', $filters['filter_by_date_order'])
                                    ->where('data_richiesta', $filters['filter_by_date_request'])
                                    ->where('stato_ordine', $filters['filter_by_date_status'])
                                    ->paginate(10);
        } else {
            $data_grid = OrderGrid::where('tipologia', 'O')->where('user_id', Auth::id())->paginate(10);
        }

How to ignore null values in query from filters array? For example the user wants to filter the record only by ID or only by ID and status, etc.. As you can see i tried to use array_filter to get only the results with values, but then if there isn't at least one filter applied i get array key error. My main question : how to skip null values in where condition?

this is how the array looks when no filters are applied from input

^ array:5 [▼
  "filter_by_id" => null
  "filter_by_client" => null
  "filter_by_date_order" => null
  "filter_by_date_request" => null
  "filter_by_date_status" => null
]

0 likes
2 replies
shaddark's avatar

@SilenceBringer Thank you that is perfect for my need! P.s. there is a weird behaviour on status option with value 0.. like the query is not working filtering by value 0

Please or to participate in this conversation.