Monday, March 13, 2017

Performing a simple seach using where() in Laravel

I am trying to perform a search that uses checkboxes to look up results in my model, then displays them on the page.

My routes:

Route::get('parts/cpu', 'PartsController@cpuList');
Route::get('parts/cpu/search', 'PartsController@searchCpu');

My controllers:

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'));
}

My view:

<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/"></a></li>
            @endforeach
        </ul>

Im getting the following error: No query results for model [App\Processor].

This is the url being displayed on search: /parts/cpu/search?tech=Intel



via Rick

Advertisement