在 Laravel 中对分页变量进行排序
Sort a Paginated variable in Laravel
我正在尝试对 Laravel 中的分页查询结果进行排序。我需要通过将所有内容降到我的分页变量上来进行排序。
示例:
//get some data attach to variable
$variable = DB::table('exampletable')
->where('id',$somevariable)
->select('id','name')
->paginate(10);
//this function will send the variable and attach **total** on each object
$variable = $this->aFunction($variable);
//What I am trying to do, THIS is where I have to sort in the data flow
$variable->sortBy('total', 'desc');
//return data in json format
return response()->json($variable);
我试过按上面所说的那样对它进行排序,但我最终得到的变量只是在每个 segment/object 上都有名称。我已经尝试过了,这是我不断得到的结果:
{
"0":{
"id": "1",
"name": "somename",
"total": "15",
},
"1":{
"id": "2",
"name": "somename2",
"total": "100",
},
"2":{
"id": "3",
"name": "somename5",
"total": "26",
},
}
我想要实现的是这个结果:
"current_page": 1,
"data": [
{
"id": "2",
"name": "somename2",
"total": "100",
},
{
"id": "3",
"name": "somename5",
"total": "26",
},
{
"id": "1",
"name": "somename",
"total": "15",
},
]
一般
$paginatedUsers 是此处记录的 LengthAwarePaginator 实例:
https://laravel.com/api/5.7/Illuminate/Pagination/LengthAwarePaginator.html#method_presenter
我们可以使用 setCollection
来改变基础集合。并且 items()
仅提取当前页面的对象。然后收集后我们可以随意排序。
$paginatedUsers = User::paginate(3)
$paginatedUsers->setCollection(
collect(
collect($paginatedUsers->items())->sortBy('name')
)->values()
);
完整解决方案(假设 aFunction 仍然是 returns 一个分页器对象)
//get some data attach to variable
$variable = DB::table('exampletable')
->where('id',$somevariable)
->select('id','name')
->paginate(10);
//this function will send the variable and attach **total** on each object
$variable = $this->aFunction($variable);
$variable->setCollection(
collect(
collect($variable->items())->sortByDesc('total')
)->values()
);
//return data in json format
return response()->json($variable);
我正在尝试对 Laravel 中的分页查询结果进行排序。我需要通过将所有内容降到我的分页变量上来进行排序。
示例:
//get some data attach to variable
$variable = DB::table('exampletable')
->where('id',$somevariable)
->select('id','name')
->paginate(10);
//this function will send the variable and attach **total** on each object
$variable = $this->aFunction($variable);
//What I am trying to do, THIS is where I have to sort in the data flow
$variable->sortBy('total', 'desc');
//return data in json format
return response()->json($variable);
我试过按上面所说的那样对它进行排序,但我最终得到的变量只是在每个 segment/object 上都有名称。我已经尝试过了,这是我不断得到的结果:
{
"0":{
"id": "1",
"name": "somename",
"total": "15",
},
"1":{
"id": "2",
"name": "somename2",
"total": "100",
},
"2":{
"id": "3",
"name": "somename5",
"total": "26",
},
}
我想要实现的是这个结果:
"current_page": 1,
"data": [
{
"id": "2",
"name": "somename2",
"total": "100",
},
{
"id": "3",
"name": "somename5",
"total": "26",
},
{
"id": "1",
"name": "somename",
"total": "15",
},
]
一般
$paginatedUsers 是此处记录的 LengthAwarePaginator 实例: https://laravel.com/api/5.7/Illuminate/Pagination/LengthAwarePaginator.html#method_presenter
我们可以使用 setCollection
来改变基础集合。并且 items()
仅提取当前页面的对象。然后收集后我们可以随意排序。
$paginatedUsers = User::paginate(3)
$paginatedUsers->setCollection(
collect(
collect($paginatedUsers->items())->sortBy('name')
)->values()
);
完整解决方案(假设 aFunction 仍然是 returns 一个分页器对象)
//get some data attach to variable
$variable = DB::table('exampletable')
->where('id',$somevariable)
->select('id','name')
->paginate(10);
//this function will send the variable and attach **total** on each object
$variable = $this->aFunction($variable);
$variable->setCollection(
collect(
collect($variable->items())->sortByDesc('total')
)->values()
);
//return data in json format
return response()->json($variable);