mysql - 尝试在 Laravel 中进行多输入搜索
问题描述
我正在尝试制作一个laravel search
有 4 列的。我想做的是,如果用户使用一个输入字段进行搜索,他们将获得相关结果,如果用户使用多个搜索输入字段,他们将获得相关结果。
这是我的表格:
<form action="{{ route('properties-list') }}" method="POST" role="search">
@csrf
<div class="row">
<div class="col-lg-3 col-md-6 col-sm-6 col-6">
<div class="form-group">
<input style="height: 48px;" id="title" name="title" type="text" class="input-text form-control"
placeholder="Search by Title">
</div>
</div>
<div class="col-lg-3 col-md-6 col-sm-6 col-6">
<div class="form-group">
<select class="selectpicker search-fields" name="type">
<option value=""> Category </option>
<option value="Apartments"> Apartments </option>
<option value="Houses"> Houses </option>
<option value="Commercial"> Commercial </option>
<option value="Garages"> Garages </option>
</select>
</div>
</div>
<div class="col-lg-3 col-md-6 col-sm-6 col-6">
<div class="form-group">
<input style="height: 48px;" id="datepicker" name="created_at" type="text"
class="input-text form-control" placeholder="Date of Posting" autocomplete="false">
</div>
</div>
<div class="col-lg-3 col-md-6 col-sm-6 col-6">
<div class="form-group">
<input style="height: 48px;" id="search_term" name="location" type="text" class="input-text form-control" placeholder="Search City,Area">
</div>
</div>
</div>
<div class="row p-3">
<div class="col-lg-12 col-md-12 col-sm-6 col-12">
<div class="form-group">
<button class="search-button">Search</button>
</div>
</div>
</div>
</form>
这是我的控制器:
public function homeSearch(Request $request)
{
$title = $request->title;
$type = $request->type;
$created_at = $request->created_at;
$location = $request->location;
if (empty($title) && empty($type) && empty($created_at) && empty($location)) {
Session::flash('danger', "You didn't select any search any search.");
return redirect()->back();
}
$properties = Property::where('title', 'LIKE', '%' . $title . '%')
->orWhere('type', $type)
->orWhere('location', 'LIKE', '%' . $location . '%')
->orWhere('created_at', 'LIKE', '%' . $created_at . '%')
->get();
dd($properties);
}
如果我曾经搜索标题“Note”,则从以下逻辑:我已经将以下标题保存在我的数据库中
想买Note 10 plus
出售苹果手机
我想卖掉我的note 4
如果我使用 'where' 而不是 'orWhere',我会在我的 dd($properties) 中获得所有 3 个对象,有时它会给我我想要的结果,有时它会返回一个空对象。
这是参考搜索: https ://yts.mx/browse-movies
解决方案
使用 when 子句
见:https ://laravel.com/docs/7.x/collections#method-when
$properties = Property::where('title', 'LIKE', '%' . request()->title . '%')
->when(request()->type, function($query) {
$query->where('type', request()->type)
})
->when(request()->location, function($query) {
$query->where('location', 'LIKE', '%' . request()->location . '%')
})
->when(request()->created_at, function($query) {
$query->where('created_at', 'LIKE', '%' . request()->created_at . '%')
})->get();
推荐阅读
- php - 通过正则表达式解析 HTML 最深匹配优先
- asp.net-core - 缺少 Xtrareports 样式表
- ms-office - 检查单元格开头是否有单引号
- node.js - Express-session 没有设置cookie?
- javascript - Python请求有效,ajax无效
- xml - 应用程序脚本 xml 导入不适用于网站
- reactjs - 如何根据反应中的另一种输入表单自动更新一个输入表单?
- node.js - 邮递员中的 POST s3 预签名 url 调用返回 SignatureDoesNotMatch
- r - R中的数据框操作:在数字和字母之间以单个顺序分隔多个项目
- python - 动态改变python导入优先级