我試圖執行搜索,使用複選框在我的模型中查找結果,然後將它們顯示在頁面上。使用Laravel中的where()執行簡單的搜索
我的路線:
Route::get('parts/cpu', '[email protected]');
Route::get('parts/cpu/search', '[email protected]');
我的控制器:
public function cpuList(){
$processors = Processor::all();
return view('parts.cpuList', compact('processors'));
}
public function searchCpu(Request $request){
$processors = Processor::query();
$processors->where('tech', '=', $request->input('tech'))->get();
return view('parts.cpuList', compact('processors'));
}
我的觀點:
<form method="GET" action="/parts/cpu/search">
<div class="checkbox">
<label><input type="checkbox" name="tech" value="Intel">Intel</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="tech" value="AMD">AMD</label>
</div>
<input value="Submit" type="submit" class="pull-right btn btn-primary">
</form>
<ul class="list-group">
@foreach ($processors as $processor)
<li class="list-group-item clearfix"><a href="/parts/cpu/{{$processor->id}}">{{$processor->name}}</a></li>
@endforeach
</ul>
林得到以下錯誤:否查詢結果模型[應用\處理器] 。
這是被顯示在搜索的網址:?/配件/ CPU /搜索技術=英特爾
它仍然沒有提供查詢結果 – Rick
無論如何都沒有兩個查詢。 –
我的錯誤是,沒有2個查詢,但他從處理器獲取所有不需要的數據。 @SanzeebAryal – Onix