分页不适用于POST动作laravel 5

Ang*_*zar 5 pagination laravel-5

嘿伙计们在Laravel 5中使用分页时遇到问题.我希望对从搜索中获得的结果进行分页,我的代码可以工作并显示与我的搜索匹配的内容,但是当我想查看其他结果时(page = 2)它没有显示任何内容,我得到一个路由异常.

MethodNotAllowedHttpException in RouteCollection.php line 207:
Run Code Online (Sandbox Code Playgroud)

分页只适用于GET操作吗?

我会提供一些帮助.

到目前为止这是我的代码

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);
    }
Run Code Online (Sandbox Code Playgroud)

Route.php

    Route::post('results',[
        'as' => 'results_path',
        'uses' => 'SearchController@results' ]);
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

sha*_*ddy 12

是的分页仅适用于get参数.

您应该使用GET搜索页面的方法.POST请求不是为了显示数据.为什么?原因有很多,但为了简短起见,我将举两个例子:

  1. 使用GET参数,假设您在第六页 - 您可以复制链接并将其粘贴给朋友,他将能够查看与您相同的内容.随着POST这是不可能的.

  2. POST如果您设法让分页工作,则不能对请求使用后退按钮.

POST 例如,当您需要向服务器提交数据时,请求非常有用,以便创建新记录.

因此,我建议您将路线类型更改为GET您的搜索表单方法GET.


reb*_*vid 5

实际上,您可以将表单与 POST 方法一起使用。分页链接是 GET 请求,但这是自动的,您可以使用多种方法放置路由定义,并且功能将是自动的。

你的 route.php 这样的东西应该可以工作。

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


小智 5

DB::table($table)->paginate($perPage,['*'],'page',$page);
Run Code Online (Sandbox Code Playgroud)

使用它在后期工作