Laravel - 刀片视图中的分页模型排序链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37515601/
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
Laravel - paginated model sort link in blade view
提问by Agu V
In laravel 5.2 I have a paginated table of a model.
在 laravel 5.2 中,我有一个模型的分页表。
@CityController
@城市控制器
public function index(Request $request){
$cities = City::orderBy('name');
return view('pages.city.index', ["cities" => $cities ->paginate(25)]);
}
This works fine, but when I try to sort the results inside the blade view does not work.
这工作正常,但是当我尝试对刀片视图中的结果进行排序时不起作用。
@index.blade.php
@index.blade.php
<table>
<thead>
<tr class="sui-columnheader">
<th class="sui-headercell" data-field="Id">
<a href="{!! $cities->appends(['sort' => 'id'])->links() !!}">Id</a>
</th>
</thead>
<tbody class="list">
@foreach ($cities as $city)
<tr class="sui-row">
<td class="sui-cell id">{!! $city->id !!}</td>
</tr>
</tbody>
</table>
When I click the sort button just reloads the page but no sort is applied.
当我单击排序按钮时,只会重新加载页面,但不会应用排序。
Is because of the "orderBy" clause? How can I make it work and defaults to order by name? Am I missing something?
是因为“orderBy”条款吗?我怎样才能让它工作并默认按名称排序?我错过了什么吗?
回答by lagbox
Something like this perhaps. You would want to make sure you limit what can be passed to that orderBy
. A URL like "...something?sort=blah
" would cause orderBy('blah')
, which your table doesn't have, which will cause a DB error.
也许是这样的。您需要确保限制可以传递给那个的内容orderBy
。像“ ...something?sort=blah
”这样的URL会导致orderBy('blah')
,而您的表没有,这将导致数据库错误。
public function index(Request $request)
{
$cities = City::orderBy($request->input('sort', 'name'))->paginate(25);
return view('pages.city.index', ['cities' => $cities]);
}
{{ $cities->appends(Request::query())->render() }}
Just giving you a functional example, though you will have to adjust based on your own rules.
只是给您一个功能示例,但您必须根据自己的规则进行调整。
The appends
is just allowing you to continue to paginate based on the current sorting, by passing the sort option via the query string of the pagination links.
这appends
只是允许您继续根据当前排序进行分页,方法是通过分页链接的查询字符串传递排序选项。
Update:
更新:
Using your example where there is a default, but imagining there can be more than just 1 other field to sort by:
使用您的示例,其中有一个默认值,但想象可以有不止 1 个其他字段可供排序:
$sort = trim($request->input('sort'));
// if it is in the acceptable array use it, otherwise default to 'name'
$sort = in_array($sort, ['id', ...]) ? $sort : 'name';
$cities = City::orderBy($sort)->paginate(25);
<table>
<tr>
<th><a href="{{ Request::fullUrlWithQuery(['sort' => 'id']) }}">ID</a></th>
<th><a href="{{ Request::url() }}">NAME</a></th>
...
</tr>
@foreach ($cities as $city)
<tr>
<td>{{ $city->id }}</td>
<td>{{ $city->name }}</td>
...
</tr>
@endforeach
...
{{ $cities->appends(Request::query())->render() }}
Just an example, you can do this how you would like.
只是一个例子,你可以按照自己的意愿进行。