我是laravel 5.4的新手,我需要创建一个多属性search.i只能搜索单个属性。但我找不到正确的方法。
以下是与之相关的观点。
<div class="form-group">
<!-- <label>Select Your Institute</label> -->
<label>Search By</label>
<select name="institute" id="institute">
<option selected="selected" value="Trainee Id">Trainee Id</option>
<option value="Trainee Name">Trainee Name</option>
<label for="Search">Name</label>
</select>
<form action="search" method="post" class="form-inline">
<input type="text" name="search" />
<br>
<input type="hidden" value="{{ csrf_token() }}" name="_token" />
<input type="submit" name="submit" value="Search">
</form>
</div>
这是我需要对上面的视图进行修改的控制器,特别是在下拉列表中。
public function search_code(Request $request){
$query = $request->search;
$customers = DB::table('registerdetails')->where('id', 'LIKE',"%$query%")->get();
return view('registeredusers.index')->with('customers',$customers);
}
有人能建议我从下拉菜单中选择的控制器吗?
答案 0 :(得分:1)
将选择下拉列表放在<form>
中,以便您可以在控制器中使用$request->institute
检索它。然后根据所选下拉列表的值(id或name),进行查询。
<form action="search" method="post" class="form-inline">
<select name="institute" id="institute">
<option selected="selected" value="id">Trainee Id</option>
<option value="name">Trainee Name</option>
<label for="Search">Name</label>
</select>
<input type="text" name="search" /><br>
<input type="hidden" value="{{ csrf_token() }}" name="_token" />
<input type="submit" name="submit" value="Search">
</form>
在您的控制器中:
public function search_code(Request $request){
$query = $request->search;
$queryType = $request->institute; // 'id' or 'name'
$customers = DB::table('registerdetails');
if($queryType == 'id'){
$customers = $customers->where('id', 'LIKE',"%$query%");
}
if($queryType == 'name'){
$customers = $customers->where('name', 'LIKE',"%$query%");
}
$customers->get();
return view('registeredusers.index')->with('customers',$customers);
}