Ad

Laravel: IF Statement Drop Down List

- 1 answer

So i currently have a drop down list where it will output a list of hotels based on its price. I now want to further develop the drop down list so that it can output a list of hotels based on price AND distance. How would i exactly create a drop down list that will output a list of hotels based on 2 or more criteria.

SearchController.php

public function index(Request $request)
 {
  $distances = DB::table('posts')->select('distance')->distinct()->get()->pluck('distance');
  $prices = DB::table('posts')->select('price')->distinct()->get()->pluck('price');

  $postsInRange = $request->has('distance')
  ? Post::where('distance', $request->distance)->get()
  : [];
  $postsInRange1 = $request->has('price')
  ? Post::where('price', $request->price)->get()
  : [];

   return view('Pages.search', [
  'distances' => $distances,
  'prices' => $prices,
  'posts' => $postsInRange,
  'posts' => $postsInRange1,
 ]);


 }

   public function store(Request $request)
     {
      // This will return all request data to your screen.
       return $request->all();
       return view('Pages.search');
     }

Search.blade.php

<select name="distance" id="distance" class="form-control input-lg dynamic" data-dependent="state">
<option value="">Distance</option>


@foreach($distances as $distance)
    <option value="{{ $distance }}">{{ $distance }}</option>
@endforeach


</select>
<br>

 <select name="price" id="price" class="form-control input-lg dynamic" data-dependent="state">
 <option value="">Price</option>


@foreach($prices as $price)
    <option value="{{ $price}}">{{ $price}}</option>
@endforeach

So to clarify the drop down list works but only outputs one of the choices selected.

Ad

Answer

I think what you need is query filter

$post = new Post;

if ($request->has('price')) {
   $post->where('price', $request->price);
}

if ($request->has('distance')) {
    $post->where('distance', $request->distance);
}

return view('Pages.search', [
  'distances' => $distances,
  'prices' => $prices,
  'posts' => $post->get()
]);

I assume that you need one list applying both filters.

Ad
source: stackoverflow.com
Ad