skip to Main Content

I am a beginner in laravel, in my form, there is a table and some lookup fields like text input, which I want to search the values in and display on the table.
When I perform a search process on a text input like name, filtering the table after enter works fine، but when I add a number of search fields, like filtering on name and city, it no longer filters on any of the name or city fields. can’t In fact, the request value in the controller returns an empty value.

Briefly,in indexfiltering.blade.php:

...
<div>
   <a href="{{ route('customers.indexfiltering')}}">search</a>
</div>
...
<div>
     <input type="text" class="form-control" id="name" name="name" value="{{ $name }}">
</div>
<div>
    <input type="text" class="form-control" id="city" name="city" value="{{ $city }}">
</div>
            

and in my controller:

public function indexFiltering(Request $request)
{
 $name= $request->query('name');
 $city= $request->query('city');
 if (!empty($name) || !empty($city)) {
    $customer= Customer::sortable()
   ->where('customer.name', 'like', '%'.$name.'%')
   ->where('customer.city', $city)
   ->paginate(8);
 } else {
   $customers = customer::sortable()
   ->paginate(8);
 }

 return view('customers.indexfiltering',
 compact('customer'))
 ->with('customers', $customers)
 ->with('name', $name)
 ->with('city', $city);
 }

The second problem is that I want filtering to be done by selecting any item from the html select. Currently this is not done and selecting any option has no effect on filtering

4

Answers


  1. You might need to start a query in the begining of the function like $query = Customer::query(); and check requests after.

    $query = Customer::query();
    
    if(request('name') && !empty(request('name'))) {
     $query->where('name', request('name'));
    }
    if(request('city') && !empty(request('city'))) {
     $query->where('city', request('city'));
    }
    
    $customers = $query->paginate(8); // or $query->get();
    
    return $customers;
    
    Login or Signup to reply.
  2. As my comment stated you are searching by name AND city always, never individually (due to your if statement).

    You could change your code to below:

    • It searches by name if name is not emtpy.
    • It searches by city if city is not empty.
    • And by both in case name and city are both not empty.
    
    $name= $request->query('name');
    $city= $request->query('city');
    
    $query = Customer::sortable();
    
    if (!empty($name) {
        $query->where('customer.name', 'like', '%'.$name.'%');
    }
    
    if (!empty($city) {
        $query->where('customer.city', $city)
    }
    
    $customers = $query->paginate(8);
    
     return view('customers.indexfiltering', compact('customer'))
         ->with('customers', $customers)
         ->with('name', $name)
         ->with('city', $city);
     }
    
    
    Login or Signup to reply.
  3. You need to modify the query to dynamically add conditions based on whether the input is set and not empty.

    public function indexFiltering(Request $request)
    {
        $query = Customer::query(); // Start the query builder chain
    
        // Check if the 'name' filter is set and not empty
        if ($request->has('name') && !empty($request->input('name'))) {
            $query->where('name', 'like', '%' . $request->input('name') . '%');
        }
    
        // Check if the 'city' filter is set and not empty
        if ($request->has('city') && !empty($request->input('city'))) {
            $query->where('city', $request->input('city'));
        }
    
        // Paginate the results
        $customers = $query->sortable()->paginate(8);
    
        // Pass the filtered customers to the view
        return view('customers.indexfiltering', [
            'customers' => $customers,
            'name' => $request->input('name'),
            'city' => $request->input('city')
        ]);
    }
    
    Login or Signup to reply.
  4. here is a slightly cleaner option using the when() that allows to remove conditionals:

    public function indexFiltering(Request $request)
    {
        //validation
    
        $query = Customer::query(); 
          ->when(request('name'),
             fn($query) => $query->where('name', request('name'))//or like...
          )->when(request('city'),
             fn($query) => $query->where('city', request('city'))
          );
    
        // Paginate the results
        $customers = $query->sortable()->paginate(8);
    
        // Pass the filtered customers to the view
        return view('customers.indexfiltering', [
          'customers' => $customers,
          'name' => $request->input('name'),
          'city' => $request->input('city')
    ]);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search