分页不适用于 POST 动作 laravel 5 [英] Pagination doesn't work with POST action laravel 5

查看:29
本文介绍了分页不适用于 POST 动作 laravel 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Laravel 5 中使用分页时遇到问题.在这种情况下,我想对从搜索中获得的结果进行分页,我的代码可以工作并显示与我的搜索匹配的内容,但是当我想查看其他内容时结果(页面 = 2)它不显示任何内容,并且我收到路由异常.

RouteCollection.php 第 207 行中的

MethodNotAllowedHttpException:

分页是否仅适用于 GET 操作?

我希望得到一些帮助.

这是我目前的代码

SearchController.php

/* 带上表格 */公共函数索引(){$levels = Level::all();return view('search.seek')->with('levels',$levels);}/*** 显示与我的搜索匹配的结果* @param 请求 $request* @return 响应*/公共函数结果(请求 $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',['作为' =>'结果路径','使用' =>'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">{!!AppTopic::find($suggestion->topic)->name !!}</h3>

<div class="col-sm-3"><h4 class="pull-right"><small><em>{!!$suggestion->created_at->format('Y-m-d') !!}</em></small>

<div class="panel-body">{!!$suggestion->how_to_use !!}</div><div class="panel-footer"><span class="label label-default">Por: {!!AppUser::find($suggestion->user_id)->name !!}</span><span class="label label-success pull-right">{!!link_to('results/' .$suggestion->id,'Ver',null) !!}</span></div>

@endforeach

@endforeach{!!$suggestions->render() !!}@万一

解决方案

是的,分页仅适用于获取参数.

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

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

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

POST 请求在您需要向服务器提交数据时很有用,例如,为了创建新记录.

所以我建议你把路由类型改成GET,把搜索表单方法改成GET.

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">{!! AppTopic::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: {!! AppUser::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

解决方案

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.

这篇关于分页不适用于 POST 动作 laravel 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆