Pagination for search results laravel 5.3

前端 未结 10 1377
无人及你
无人及你 2021-02-01 06:17

Pagination search results

I have just started with Laravel and I am trying to make a search function with proper pagination. The function works for page one but on pag

10条回答
  •  伪装坚强ぢ
    2021-02-01 07:09

    I assume you want to change pages with urls like this search/1, search/2? First of all your route should be probably Route::post('search/{page?}').

    I'm not sure if only this change will work, but if it does not, you have to resolve page like this

    public function search(\Illuminate\Http\Request $request, $page = 1)
    {
        $q = $request->get('search');
    
        \Illuminate\Pagination\Paginator::currentPageResolver(function () use ($page) {
            return $page;
        });
    
        # going to next page is not working yet
        $product = Product::where('naam', 'LIKE', '%' . $q . '%')
            ->orWhere('beschrijving', 'LIKE', '%' . $q . '%')
            ->paginate(6);
    
        return view('pages.index', compact('product'));
    }
    

提交回复
热议问题