首页 > 解决方案 > Laravel - 下拉列表 where 语句

问题描述

我目前有一个下拉列表,当我选择值时,它将显示数据。但是,我现在添加了一个新的下拉列表,但是当我按下提交时,什么都没有出现。

我选择距离并按下提交数据。但是,我现在已经包含了一个价格下拉列表,但是当我按下提交时什么都没有出现。有人可以帮忙吗,见代码:

搜索控制器.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()
 : [];

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

搜索.php

<div class="form-group">

 <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


 </select>

标签: laravel

解决方案


尝试这个:

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

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

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

推荐阅读