user7110057
user7110057

Reputation:

Query building for filter based on select dropdown in laravel

I'm a bit starter in the Laravel system so please bear with me.

Currently I have stored images in the database and they have columns featured and approved. They both have status 0, and 1. Approved/Not approved.

What I'm trying to to is to be able to filter from the admin dashboard:

This is what I have in my index.blade.php

<div class="col-md-2">
   <div class="form-group">
      <label for="featured">Featured</label>
          <select class="form-control" name="featured" id="featured">
          <option value=""> All</option>
          <option value="1">Yes</option>
          <option value="0">No</option>
      </select>                 
   </div>            
</div>
<div class="form-group">
   <label for="approved">Approved</label>
       <select class="form-control" name="approved" id="approved">
            <option value=""> All</option>
            <option value="1">Yes</option>
            <option value="0">No</option>
       </select>                 
</div> 
<button type="submit" class="btn btn-primary">Search</button>

And this is the query that I'm trying to build in the controller

public function index(Request $request)
{        
    $approved = $request->approved;
    $featured = $request->featured;
    
    $images = Images::when($approved, function($query, $approved) {
        if ($approved == '0') {
            return $query->where('approved', 0);
        } else if ($approved == '1' ){
            return $query->where('approved', 1);
        } else {
            return $query->where('approved', [0,1]);
        }           
    })
    ->when($featured, function($query, $featured) {
        if ($featured == '0') {
            return $query->where('featured', 0);
        } else if ($featured == '1' ){
            return $query->where('featured', 1);
        } else {
            return $query->where('featured', [0,1]);
        }   
    })
    ->latest()->get();
    
    return view( .... );
}

This is what I have in route

Route::resource('images', 'ImagesController')->except('show');

First problem is that when I choose option "All" it doesn't show anything, and when I choose "No" or "Yes" it shows same results.

I also don't think that this is the correct way of building such query.

Upvotes: 2

Views: 4832

Answers (1)

Wael Khalifa
Wael Khalifa

Reputation: 935

Just try this code hop it will help you

<form action="{{route('images.index')}}">
        <div class="col-md-2">
            <div class="form-group">
                <label for="featured">Featured</label>
                <select class="form-control" name="featured" id="featured">
                    <option {{is_null(request()->input('featured')) ? 'selected' : ''}} value=""> All</option>
                    <option {{request()->input('featured') == 1 ? 'selected' : ''}} value="1">Yes</option>
                    <option {{!is_null(request()->input('featured')) && request()->input('featured') == 0 ? 'selected' : ''}} value="0">No</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label for="approved">Approved</label>
            <select class="form-control" name="approved" id="approved">
                <option {{is_null(request()->input('approved')) ? 'selected' : ''}} value=""> All</option>
                <option {{request()->input('approved') == 1 ? 'selected' : ''}} value="1">Yes</option>
                <option {{!is_null(request()->input('approved')) && request()->input('approved') == 0 ? 'selected' : ''}} value="0">No</option>
            </select>
        </div>
        <button type="submit" class="btn btn-primary">Search</button>
    </form>

public function index(Request $request)
{        
    $approved = $request->approved;
    $featured = $request->featured;
    
$images = Images::when(!is_null($approved), function ($query) use ($approved) {
            return $query->where('approved', $approved);
        })->when(!is_null($featured), function ($query) use ($featured) {
            return $query->where('featured', $featured);
        })->latest()->get();
    
    return view( .... );
}

Upvotes: 1

Related Questions