php 搜索结果 Laravel 5.3 的分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39326206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pagination for search results laravel 5.3
提问by Casper Spruit
Pagination search results
分页搜索结果
I have just started with Laravel and I am trying to make a search function with proper pagination. The function works for page one but on page two it doesn't. I think it's not giving the results to the next page but I can't seem to find an answer.
我刚刚开始使用 Laravel,我正在尝试使用适当的分页来制作搜索功能。该功能适用于第一页,但在第二页上不起作用。我认为它没有将结果提供给下一页,但我似乎无法找到答案。
this is my search function inside IndexController:
这是我在 IndexController 中的搜索功能:
public function search()
{
$q = Input::get('search');
# going to next page is not working yet
$product = Product::where('naam', 'LIKE', '%' . $q . '%')
->orWhere('beschrijving', 'LIKE', '%' . $q . '%')
->paginate(6);
return view('pages.index', compact('product'));
}
this is my route:
这是我的路线:
Route::post('search{page?}', 'IndexController@search');
this is the URL of page two:
这是第二页的网址:
/search?page=2
this is how I show my pagination:
这就是我显示分页的方式:
{{ $product->appends(Request::get('page'))->links()}}
the error:
错误:
MethodNotAllowedHttpException in RouteCollection.php line 218:
Get error on request.
根据要求获取错误。
Route:
路线:
Route::get('search/{page?}', 'IndexController@search');
Error:
错误:
MethodNotAllowedHttpException in RouteCollection.php line 218:
in RouteCollection.php line 218
at RouteCollection->methodNotAllowed(array('GET', 'HEAD')) in RouteCollection.php line 205
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 780
at Router->findRoute(object(Request)) in Router.php line 610
at Router->dispatchToRoute(object(Request)) in Router.php line 596
at Router->dispatch(object(Request)) in Kernel.php line 267
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 53
I hope my question is clear and in the right format. Thank you in advance (sorry for my bad English)
我希望我的问题很清楚并且格式正确。提前谢谢你(对不起,我的英语不好)
Answer:
回答:
I ended up using the answer of this post in combination with some help of thispost
最后我用这篇文章的答案,结合一些帮助这个职位
I used a post function for the initial search and a get function for the following pages. This was possible because I'm now giving my search to the URL.
我在初始搜索中使用了 post 函数,在后续页面中使用了 get 函数。这是可能的,因为我现在正在搜索 URL。
EDIT:
编辑:
- added the initial error.
- added the
Route::get
error - added answer
- 添加了初始错误。
- 添加了
Route::get
错误 - 添加答案
回答by Andrej Ludinovskov
If you want to apply filters to the next page you should add them to your paginator like this:
如果要将过滤器应用于下一页,则应将它们添加到分页器中,如下所示:
$product = Product::where('naam', 'LIKE', '%' . $q . '%')
->orWhere('beschrijving', 'LIKE', '%' . $q . '%')
->paginate(6);
$product->appends(['search' => $q]);
And change your route from post to get:
并更改您的邮寄路线以获得:
Route::get('search', 'IndexController@search');
回答by Joci93
Route::get('product', function () {
$product= App\product::paginate(15);
$product->setPath('custom/url');
});
View:
看法:
{{ $product->appends(['search' => Request::get('page')])->links() }}
回答by ytdm
a quick way in view (Lavarel 5.7)
快速查看方式(Lavarel 5.7)
$product->appends(Request::all())->links();
回答by Y. Joy Ch. Singha
$searchdata = \Request::get( 'inputTextFieldname' ); \make as global
$searchresult = Modelname::where ( 'blogpost_title', 'LIKE', '%' .$searchdata . '%' )->paginate(2);
return view( 'search', compact('searchresult') );
and in your view page
并在您的视图页面中
{{$searchresult->appends(Request::only('inputTextFieldname'))->links()}}
make your route to get method
让你的路线得到方法
Route::get('/search', ['as' => 'search', 'uses' => 'searchController@index']);
this will be done, thanks,
这将完成,谢谢,
回答by Skysplit
I assume you want to change pages with urls like this search/1
, search/2
? First of all your route should be probably Route::post('search/{page?}')
.
我假设您想使用这样的网址更改页面search/1
,search/2
?首先你的路线应该是可能的Route::post('search/{page?}')
。
I'm not sure if only this change will work, but if it does not, you have to resolve page like this
我不确定是否只有此更改有效,但如果无效,您必须像这样解析页面
public function search(\Illuminate\Http\Request $request, $page = 1)
{
$q = $request->get('search');
\Illuminate\Pagination\Paginator::currentPageResolver(function () use ($page) {
return $page;
});
# going to next page is not working yet
$product = Product::where('naam', 'LIKE', '%' . $q . '%')
->orWhere('beschrijving', 'LIKE', '%' . $q . '%')
->paginate(6);
return view('pages.index', compact('product'));
}
回答by sh6210
in my case, i've laravel 5.7 installed.
就我而言,我已经安装了 laravel 5.7。
$perPage = $request->per_page ?? 10;
$data['items'] = User::where('name', 'like', '%'. $request->search . '%')
->paginate($perPage)
->appends(['search' => $request->search, 'per_page' => $request->per_page]);
return view('users.index', $data);
and my view files codes are
我的视图文件代码是
for per_pageselect dropdown and searcharea
对于per_page选择下拉列表和搜索区域
<form role="form" class="form-inline" method="get" action='{{ url('/user') }}'>
<div class="row">
<div class="col-sm-6">
<div class="dataTables_length">
<label>Show
<select name="per_page"
onchange="this.form.submit()"
class="form-control input-sm">
<option value=""></option>
<option value="10" {{ $items->perPage() == 10 ? 'selected' : '' }}>10
</option>
<option value="25" {{ $items->perPage() == 25 ? 'selected' : '' }}>25
</option>
<option value="50" {{ $items->perPage() == 50 ? 'selected' : '' }}>50
</option>
</select>
entries
</label>
</div>
</div>
<div class="col-sm-6">
<div class="dataTables_filter pull-right">
<div class="form-group">
<label>Search:
<input type="search" name="search" class="form-control input-sm"
placeholder="Name" value="{{ request()->search }}">
</label>
</div>
</div>
</div>
and my pagination generator code
和我的分页生成器代码
{{ $items->appends(['search' => request()->search, 'per_page' => request()->per_page])->links() }}
回答by Amit Shah
If you are using search form with GET method then use something like these to preserve pagination withing search results.
如果您使用带有 GET 方法的搜索表单,则使用类似这些内容来保留搜索结果的分页。
public function filter(Request $request)
{
$filter = $request->only('name_operator','name_value','email_operator','email_value', 'phone_operator','phone_value', 'gender_value', 'age_operator','age_value');
$contacts = $this->repo->getFilteredList(array_filter($filter));
$contacts->appends($filter)->links(); //Continue pagination with results
return view('dashboard::index', compact('contacts'))->withInput($request->all());
}
回答by Miraj Khandaker
In your view file where you display pagination...
在显示分页的视图文件中...
{{ $results->appends(Request::except('page'))->links() }}
appends keeps the query string value except "page".
appends 保留查询字符串值,除了“page”。
回答by ???? ?????
use any in route Instead post Route::any('search', 'IndexController@search');
在路由中使用 any 而不是 post Route::any('search', 'IndexController@search');
回答by Kemal Y
For pagination, you should create a simple form:
对于分页,您应该创建一个简单的表单:
<form action="{{URL::to('/search')}}" method="post">
<input type="hidden" name="query"/>
<select name="pages">
@for($p = 1; $p < $products->lastPage(); $p++ )
<option value="{{ $p }}">{{ $p }}</option>
@endfor
</select>
</form>
Pagination methods are here:
分页方法在这里:
$results->count()
$results->currentPage()
$results->firstItem()
$results->hasMorePages()
$results->lastItem()
$results->lastPage() (Not available when using simplePaginate)
$results->nextPageUrl()
$results->perPage()
$results->previousPageUrl()
$results->total() (Not available when using simplePaginate)
$results->url($page)