laravel - 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>
解决方案
尝试这个:
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
]);
推荐阅读
- c# - 在动态crm中将销售订单详细信息添加到销售订单
- python - sklearn:垃圾箱必须单调递增或递减
- javascript - 如何在节点应用程序中指定环境变量?
- javascript - 带有 fs 和 setInterval 的节点 js
- css - 将 prop 传递给样式化组件中的嵌套元素
- vue.js - 如何从 vue 应用程序生成包含所有节点模块的包?
- javascript - 删除然后添加子节点
- doctrine-orm - 如何使用 Doctrine DBAL、TYPO3 和 TypoScript 进行随机排序
- c++ - CPP Prime Generator SPOJ 筛
- nginx - nginx 没有显示带有 uwsgi 的自定义错误页面