Laravel:IF语句下拉列表

Laravel:IF语句下拉列表,laravel,Laravel,所以我现在有一个下拉列表,它会根据价格输出一个酒店列表。我现在想进一步开发下拉列表,以便它可以根据价格和距离输出酒店列表。我如何创建一个下拉列表,根据2个或更多标准输出酒店列表 SearchController.php public function index(Request $request) { $distances = DB::table('posts')->select('distance')->distinct()->get()->pluck('dist

所以我现在有一个下拉列表,它会根据价格输出一个酒店列表。我现在想进一步开发下拉列表,以便它可以根据价格和距离输出酒店列表。我如何创建一个下拉列表,根据2个或更多标准输出酒店列表

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

距离
@foreach($distance as$distance)
{{$distance}}
@endforeach

价格 @foreach(价格为$price) {{$price} @endforeach

因此,澄清下拉列表的工作,但只输出一个选择

我认为您需要的是查询过滤器

$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()
]);

我假设您需要一个列表来应用这两个过滤器。

我是否要取消返回视图(“Pages.search”、[“distance”=>$distance、“'prices'=>$prices,'posts'=>$postsInRange,'posts'=>$postsInRange1,]);或者我要保留这个,在那之后,我现在得到了一个错误:非静态方法illumb\Database\elount\Model::newQuery()不应该被静态调用,这是我以前没有遇到过的(我的laravel技能不是最好的)Hi@MikeRoss非常好用,但是只是遇到了一个小问题。比如说,如果我想只根据价格列出所有酒店,我将如何做到这一点。目前,它只在我同时选择价格和距离时起作用,但如果我只想列出所有价格,当我这样做时,它不会显示任何内容,并且过滤器也不起作用/在控制器中检查
$request->('price')
的值是多少,如果该值为零或0,则不要运行
price
查询。