在Laravel 5中使用分页时,我遇到问题。在这种情况下,我想对从搜索中获得的结果进行分页,我的代码可以工作并显示与搜索匹配的内容,但是当我想查看其他结果时(页面= 2)它什么也不显示,并且出现路由异常。
MethodNotAllowedHttpException in RouteCollection.php line 207:
分页仅适用于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);
}
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
是,分页仅适用于get参数。
您应该GET
在搜索页面中使用方法。POST
请求并非旨在显示数据。为什么?原因有很多,但总之,我将举两个例子:
使用GET
参数,假设您位于第六页-您可以复制链接并将其粘贴到朋友,他将可以查看与您相同的内容。有了POST
这是不可能的。
POST
如果您设法使分页正常工作,则不能将后退按钮用于请求。
POST
例如,当您需要将数据提交到服务器以创建新记录时,请求很有用。
因此,建议您将路线类型更改为GET
,将搜索表单方法更改为GET
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句