Home » Php » Pagination for search results laravel 5.3

Pagination for search results laravel 5.3

Posted by: admin November 30, 2017 Leave a comment

Questions:

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 page two it doesn’t. I think it’s not giving the results to the next page but I can’t seem to find an answer.


this is my search function inside IndexController:

public function search()
{
    $q = Input::get('search');

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

this is my route:

Route::post('search{page?}', '[email protected]');

this is the URL of page two:

/search?page=2

this is how I show my pagination:

{{ $product->appends(Request::get('page'))->links()}}

the error:

MethodNotAllowedHttpException in RouteCollection.php line 218:

Get error on request.

Route:

Route::get('search/{page?}', '[email protected]');

Error:

MethodNotAllowedHttpException in RouteCollection.php line 218:
in RouteCollection.php line 218
at RouteCollection->methodNotAllowed(array('GET', 'HEAD')) in RouteCollection.php line 205
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 780
at Router->findRoute(object(Request)) in Router.php line 610
at Router->dispatchToRoute(object(Request)) in Router.php line 596
at Router->dispatch(object(Request)) in Kernel.php line 267
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 53

I hope my question is clear and in the right format. Thank you in advance (sorry for my bad English)


Answer:

I ended up using the answer of this post in combination with some help of this post

I used a post function for the initial search and a get function for the following pages. This was possible because I’m now giving my search to the URL.


EDIT:

  • added the initial error.
  • added the Route::get error
  • added answer
Answers:

If you want to apply filters to the next page you should add them to your paginator like this:

$product = Product::where('naam', 'LIKE', '%' . $q . '%')
        ->orWhere('beschrijving', 'LIKE', '%' . $q . '%')
        ->paginate(6);
$product->appends(['search' => $q]);

And change your route from post to get:

Route::get('search', '[email protected]');

Questions:
Answers:

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

Questions:
Answers:
Route::get('product', function () {
    $product= App\product::paginate(15);

    $product->setPath('custom/url');

});

View:

{{ $product->appends(['search' => Request::get('page')])->links() }}

Questions:
Answers:

For pagination, you should create a simple form:

<form action="{{URL::to('/search')}}" method="post">
    <input type="hidden" name="query"/>
    <select name="pages">
    @for($p = 1; $p < $products->lastPage(); $p++ )
        <option value="{{ $p }}">{{ $p }}</option>
    @endfor
    </select>
</form>

Pagination methods are here:

$results->count()
$results->currentPage()
$results->firstItem()
$results->hasMorePages()
$results->lastItem()
$results->lastPage() (Not available when using simplePaginate)
$results->nextPageUrl()
$results->perPage()
$results->previousPageUrl()
$results->total() (Not available when using simplePaginate)
$results->url($page)

Questions:
Answers:
    $searchdata =  \Request::get( 'inputTextFieldname' ); \make as global

$searchresult = Modelname::where ( 'blogpost_title', 'LIKE', '%' .$searchdata . '%' )->paginate(2);
    return view( 'search', compact('searchresult') );

and in your view page

{{$searchresult->appends(Request::only('inputTextFieldname'))->links()}}

make your route to get method

Route::get('/search', ['as' => 'search', 'uses' => '[email protected]']);

this will be done, thanks,