搜索结果的分页存在问题。我得到了第一页,但是对于第2页,我收到了这个错误:
QueryException in Connection.php line 729:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'page' in 'where clause' (SQL: select count(*) as aggregate from `books` where `id` like %% and `page` like %2% and `books`.`deleted_at` is null)
我的路线:
Route::get('books/search', 'BookController@searchForm');
Route::get('books/results', 'BookController@searchProcess');
控制器:
use Search; //search trait
public function searchForm(){
return view('books.search.form');
}
public function searchProcess(Request $request){
$result = $this->performSearch($request, $this->model);
return view('books.search.result', compact('result'));
}
搜索特征(另一个文件中的一个类):
protected function performSearch($request, $model){
$searchFields = $request->toArray();
unset($searchFields['_token']);
$searchFields = array_filter($searchFields);
if (empty($searchFields)) {
return back();
}
$list = $model->where('id', 'like', '%%');
foreach ($searchFields as $column => $value) {
$list->where($column, 'like', '%'.$value.'%');
}
return $list->paginate(10);
}
显示结果的视图:
<div class="table-responsive">
<table class="table table-hover table-bordered">
<tr>
<th>Results</th>
</tr>
@foreach($result as $datum)
<tr>
<td><a href="{{route('books.show', $datum->id)}}">{{$datum->title}}</a></td>
</tr>
@endforeach
</table>
</div>
@if($result->lastPage() > 1)
<nav class="text-center">
{{$result->links()}}
</nav>
@endif
我正在使用Laravel 5.2
答案 0 :(得分:1)
似乎你在查询中传递页面参数,所以我认为你需要从查询构建中删除页面参数
protected function performSearch($request, $model)
{
$searchFields = $request->except(['_token', 'page']); // get all fields except _token and page you can add more parameters in array if they are in request but not in database table column
$searchFields = array_filter($searchFields);
if (empty($searchFields)) {
return back();
}
$list = $model->where('id', 'like', '%%');
foreach ($searchFields as $column => $value) {
$list->where($column, 'like', '%'.$value.'%');
}
return $list->paginate(10);
}