问题描述
伙计们,我在Laravel 5中使用分页时遇到问题.在这种情况下,我想对我从搜索中获得的结果进行分页,我的代码有效并显示与我的搜索相匹配的内容,但是当我想查看时其他结果(页面= 2)没有显示任何内容,并且出现路由异常.
Hey guys 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:
分页仅适用于GET操作吗?
Does pagination only work with a GET action?
我很乐意提供帮助.
到目前为止,这是我的代码
This is my code so far
SearchController.php
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.php
Route::post('results',[
'as' => 'results_path',
'uses' => 'SearchController@results' ]);
list.blade.php
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
推荐答案
是的,分页仅适用于get参数.
Yes pagination only works with get parameters.
对于搜索页面,应使用GET
方法. POST
请求并非旨在显示数据.为什么?原因很多,但总之,我将举两个例子:
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:
-
使用
GET
参数,假设您位于第六页-您可以复制链接并将其粘贴到朋友,他将可以查看与您相同的内容.对于POST
,这是不可能的.
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. WithPOST
this is impossible.
如果设法使分页正常工作,则不能将后退按钮用于POST
请求.
You can not use back button with POST
requests, if you manage to get pagination to work.
例如,当您需要向服务器提交数据以创建新记录时,
POST
请求非常有用.
POST
requests are useful when you need to submit data to the server, in order to create new record, for example.
因此,我建议您将路线类型更改为GET
,并将搜索表单方法更改为GET
.
So I suggest you to change your route type to GET
and your search form method to GET
.
这篇关于分页不适用于POST动作laravel 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!