Selo's avatar
Level 1

Error in paginating search results

Hi,

I have a problem with the pagination of search results. I get the first page right, but for page 2 I get this error:

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:

Route::get('books/search', 'BookController@searchForm');
Route::get('books/results', 'BookController@searchProcess');

Controller:

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

Search Trait:

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

I am using Laravel 5.2

0 likes
1 reply
MaxMatteo's avatar

do this too : unset($searchFields['page']);

dont add where query with your pagination page parameter ;)

cheers max

Please or to participate in this conversation.