9

I have an issue when using pagination in Laravel 5. This is the case I want to paginate results that I get from a search, my code works and displays the content that matches my search, but when I want to see the others results (page = 2) it doesn't display anything and I get a route exception.

MethodNotAllowedHttpException in RouteCollection.php line 207:

Does pagination only work with a GET action?

I would apreciate some help.

This is my code so far

SearchController.php

/* Bring the form */
public function index()
    {
        $levels = Level::all();
        return view('search.seek')->with('levels',$levels);
    }

    /**
     * Display results that matches my search
     * @param Request $request
     * @return Response
     */
    public function results(Request $request)
    {
        $suggestions = Suggestion::where('subject','=',$request->subject,'and')
                                    ->where('level','=',$request->level,'and')
                                    ->where('grade','=',$request->grade,'and')
                                    ->where('topic','=',$request->topic,'and')
                                    ->where('device','=',$request->device)->latest()->paginate(5);

        $suggestions->setPath('results');
        return view('search.list')->with('suggestions', $suggestions);
    }

Route.php

    Route::post('results',[
        'as' => 'results_path',
        'uses' => 'SearchController@results' ]);

list.blade.php

@if($suggestions != null)
        <h1 class="page-heading">Resultados</h1>
        @foreach($suggestions->chunk(5) as $Suggestions)
        <div class="row">
            <div class="col-lg-12">
                    @foreach($Suggestions as $suggestion)
                        <div id="postlist">
                            <div class="panel">
                                <div class="panel-heading">
                                    <div class="text-center">
                                        <div class="row">
                                            <div class="col-sm-9">
                                                <h3 class="pull-left">{!! \App\Topic::find($suggestion->topic)->name !!}</h3>
                                            </div>
                                            <div class="col-sm-3">
                                                <h4 class="pull-right">
                                                    <small><em>{!! $suggestion->created_at->format('Y-m-d') !!}</em></small>
                                                </h4>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="panel-body">{!! $suggestion->how_to_use !!}</div>
                                <div class="panel-footer"><span class="label label-default">Por: {!! \App\User::find($suggestion->user_id)->name !!}</span><span class="label label-success pull-right">{!! link_to('results/' .$suggestion->id ,'Ver',null) !!}</span></div>
                            </div>
                        </div>
                    @endforeach
            </div>
        </div>
        @endforeach
        {!! $suggestions->render() !!}
    @endif
peterh
  • 11,875
  • 18
  • 85
  • 108
AngelSalazar
  • 3,080
  • 1
  • 16
  • 22

5 Answers5

22

Yes pagination only works with get parameters.

You should use GET method for your search page. POST requests aren't meant for the purpose of displaying data. Why? There are many reasons, but to be short I will give you two examples:

  1. With GET parameters, let's say you are on sixth page - you can copy the link and paste it to friend and he will be able to view the same content as you. With POST this is impossible.

  2. You can not use back button with POST requests, if you manage to get pagination to work.

POST requests are useful when you need to submit data to the server, in order to create new record, for example.

So I suggest you to change your route type to GET and your search form method to GET.

Sh1d0w
  • 9,340
  • 3
  • 24
  • 35
  • Thanks buddy, your explanation is useful to me, I have already changed it to GET resquest, it works, but it only retrieves the first 5 records and I still can't see the other ones, it shows nothing when using the pagination buttons, I'll keep checking – AngelSalazar May 28 '15 at 20:30
  • 1
    you convinced me too to switch to get for search instead of post, thanks – niko craft Oct 28 '16 at 08:46
  • @AngelSalazar, is your query string being maintained when you use the pagination buttons? Try $suggestions->appends(Request::except('page'))->links() for the pagination links – Menasheh Jul 23 '17 at 20:44
  • There are also problems, with GET request parameters. Biggest of them is, that numbers passed as query strings are converted to strings. This does not happen when params are passed in POST request body. – Tola Mar 19 '18 at 07:19
6

Really you can use your form with POST method. The pagination links are GET request, but this is automatic, you can put the route definition with multiple methods and the functionality is going to be automatic.

Something like this for your route.php should work.

Route::match(['get', 'post'], 'results',[
        'as' => 'results_path',
        'uses' => 'SearchController@results' ]);
rebduvid
  • 1,104
  • 10
  • 13
  • 1
    Nice one. Works even in Laravel 6. You have to also mind to add following to the view $data->appends(Request::all())->links() – ronline May 04 '21 at 14:07
5
DB::table($table)->paginate($perPage,['*'],'page',$page);

use this its gonaa work in post

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
hafiz
  • 61
  • 1
  • 1
2

Here are the complete answer

  • Web.php
    Route::get('/faq', 'MainController@faq')->name('faq');
    
    Route::any('/faq-search', 'MainController@faqSearch')->name('faqSearch');

  • Controller

      public function faq()
    {
        $faqs = Faq::paginate(5);

        return view('guest.faq', compact('faqs'));
    }

    public function faqSearch(Request $request)
    {
        $faqSearch = $request->get('faqSearch');

        $faqs = Faq::where('en_question', 'like', $faqSearch.'%')->paginate(5)->setPath('');

        $pagination = $faqs->appends(array(
          'faqSearch' => $faqSearch
        ));

        return view('guest.faq', compact('faqs', 'faqSearch'));
    }

  • View (Blade)
    
              <form action="{{route('faqSearch')}}" method="post">
                @csrf
    
                  <div class="input-group flex-nowrap">
                      <div class="input-group-prepend">
                          <button class="btn btn-primary input-group-text" type="submit">
                              <i class="fa fa-search"></i>
                          </button>
                      </div>
                      <input type="text" name="faqSearch" class="form-control" placeholder="Search" aria-label="Search" aria-describedby="addon-wrapping" value="{{ old('faqSearch', $faqSearch ?? '') }}" >
                  </div>
              </form>
    
                <div class="col-md-3 mx-auto">
                  {{ $faqs->links() }}
              </div>
    
    

Thank you so much to "Avinash Nethala" for your blog

Resource

Hassan Joseph
  • 87
  • 2
  • 8
0

Solution is to use JS or jquery for returned all data from controller in view.

stef
  • 1
  • 1