Pagination not working with laravel 5 POST action

Hi guys, I have a problem when using pagination in Laravel 5. This is the case when I want to paginate the results obtained from a search, my code works and displays the content that matches my search, but when I want to see other results (page = 2) displays nothing and I get a route exception.

MethodNotAllowedHttpException in RouteCollection.php line 207:

      

Does pagination only work with a GET action?

I would use 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

      

+3


source to share


2 answers


Yes, pagination only works with receive parameters.

You should use the method GET

for your search page. Queries are POST

not meant to display data. What for? There are many reasons, but to be short I will give you two examples:

  • With options GET

    , let's say you're on page six - you can copy the link and paste it to a friend and they can view the same content as you. This POST

    is impossible.

  • You cannot use the back button with queries POST

    if you manage to do the pagination.



POST

queries are useful when you need to send data to the server, for example to create a new record.

Therefore, I suggest you change the route type to GET

and your search form method to GET

.

+8


source


Indeed, you can use your form with the POST method. Pagination links are a GET request, but this is automatic, you can put a route definition in multiple methods and the functionality will be automatic.

Something like this for your route.php should work.



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

      

+1


source







All Articles