分页不适用于 POST 操作 laravel 5
Pagination doesn't work with POST action laravel 5
我在 Laravel 5 中使用分页时遇到问题。在这种情况下,我想对从搜索中获得的结果进行分页,我的代码可以正常工作并显示与我的搜索相匹配的内容,但是当我想查看其他结果(page = 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
参数,假设您在第六页 - 您可以复制link并粘贴到朋友,他将能够查看相同的内容和你一样。使用 POST
这是不可能的。
如果您设法使分页正常工作,则不能对 POST
请求使用后退按钮。
POST
当您需要向服务器提交数据以创建新记录时,请求很有用。
所以我建议您将路线类型更改为 GET
,将搜索表单方法更改为 GET
。
真的,您可以使用带有 POST 方法的表单。分页链接是GET请求,但这是自动的,你可以把路由定义用多种方法,功能将是自动的。
你的 route.php 这样的东西应该有用。
Route::match(['get', 'post'], 'results',[
'as' => 'results_path',
'uses' => 'SearchController@results' ]);
DB::table($table)->paginate($perPage,['*'],'page',$page);
使用这个它会在 post
中工作
解决方案是使用JS或jquery从视图中的控制器返回所有数据。
这里是完整的答案
- Web.php
Route::get('/faq', 'MainController@faq')->name('faq');
Route::any('/faq-search', 'MainController@faqSearch')->name('faqSearch');
- 控制器
public function faq()
{
$faqs = Faq::paginate(5);
return view('guest.faq', compact('faqs'));
}
public function faqSearch(Request $request)
{
$faqSearch = $request->get('faqSearch');
$faqs = Faq::where('en_question', 'like', $faqSearch.'%')->paginate(5)->setPath('');
$pagination = $faqs->appends(array(
'faqSearch' => $faqSearch
));
return view('guest.faq', compact('faqs', 'faqSearch'));
}
- 查看 (Blade)
<form action="{{route('faqSearch')}}" method="post">
@csrf
<div class="input-group flex-nowrap">
<div class="input-group-prepend">
<button class="btn btn-primary input-group-text" type="submit">
<i class="fa fa-search"></i>
</button>
</div>
<input type="text" name="faqSearch" class="form-control" placeholder="Search" aria-label="Search" aria-describedby="addon-wrapping" value="{{ old('faqSearch', $faqSearch ?? '') }}" >
</div>
</form>
<div class="col-md-3 mx-auto">
{{ $faqs->links() }}
</div>
非常感谢“Avinash Nethala”的博客
资源
我在 Laravel 5 中使用分页时遇到问题。在这种情况下,我想对从搜索中获得的结果进行分页,我的代码可以正常工作并显示与我的搜索相匹配的内容,但是当我想查看其他结果(page = 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
参数,假设您在第六页 - 您可以复制link并粘贴到朋友,他将能够查看相同的内容和你一样。使用POST
这是不可能的。如果您设法使分页正常工作,则不能对
POST
请求使用后退按钮。
POST
当您需要向服务器提交数据以创建新记录时,请求很有用。
所以我建议您将路线类型更改为 GET
,将搜索表单方法更改为 GET
。
真的,您可以使用带有 POST 方法的表单。分页链接是GET请求,但这是自动的,你可以把路由定义用多种方法,功能将是自动的。
你的 route.php 这样的东西应该有用。
Route::match(['get', 'post'], 'results',[
'as' => 'results_path',
'uses' => 'SearchController@results' ]);
DB::table($table)->paginate($perPage,['*'],'page',$page);
使用这个它会在 post
中工作解决方案是使用JS或jquery从视图中的控制器返回所有数据。
这里是完整的答案
- Web.php
Route::get('/faq', 'MainController@faq')->name('faq');
Route::any('/faq-search', 'MainController@faqSearch')->name('faqSearch');
- 控制器
public function faq()
{
$faqs = Faq::paginate(5);
return view('guest.faq', compact('faqs'));
}
public function faqSearch(Request $request)
{
$faqSearch = $request->get('faqSearch');
$faqs = Faq::where('en_question', 'like', $faqSearch.'%')->paginate(5)->setPath('');
$pagination = $faqs->appends(array(
'faqSearch' => $faqSearch
));
return view('guest.faq', compact('faqs', 'faqSearch'));
}
- 查看 (Blade)
<form action="{{route('faqSearch')}}" method="post"> @csrf <div class="input-group flex-nowrap"> <div class="input-group-prepend"> <button class="btn btn-primary input-group-text" type="submit"> <i class="fa fa-search"></i> </button> </div> <input type="text" name="faqSearch" class="form-control" placeholder="Search" aria-label="Search" aria-describedby="addon-wrapping" value="{{ old('faqSearch', $faqSearch ?? '') }}" > </div> </form> <div class="col-md-3 mx-auto"> {{ $faqs->links() }} </div>
非常感谢“Avinash Nethala”的博客
资源