Sorting 如何在sortBy和where运算符获取的集合中使用分页?
我需要为我的收藏分页,我必须应用大量的过滤器。但我无法做到这一点。我是拉威尔的新手,请给我指路。 我的控制器:Sorting 如何在sortBy和where运算符获取的集合中使用分页?,sorting,filter,pagination,laravel-5.5,Sorting,Filter,Pagination,Laravel 5.5,我需要为我的收藏分页,我必须应用大量的过滤器。但我无法做到这一点。我是拉威尔的新手,请给我指路。 我的控制器: public function index(Request $request) { $view = $request['view'] ? $request['view'] :'grid'; $purpose = $request['purpose'] ? $request['purpose'] : 'rent'; $sort = $request['so
public function index(Request $request)
{
$view = $request['view'] ? $request['view'] :'grid';
$purpose = $request['purpose'] ? $request['purpose'] : 'rent';
$sort = $request['sort'] ? $request['sort'] : 'asc';
$properties = $properties->where('purpose' , $purpose);
if($sort == 'asc');
$properties = $properties->sortBy('price');
else
$properties = $properties->sortByDesc('price');
$properties = $properties->paginate(5);
return view('frontend.properties.index', [ 'view'=>$view , 'properties' => $properties , 'request'=> $request->all() ]);
}
我遇到了这个问题,为了解决这个问题,我创建了一个名为PaginateCollection的特性:
/*
* Paginate the Laravel Collection before and/or after filtering.
*
*/
trait PaginateCollection
{
/**
* Paginate the collection.
*
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Collection $collection
* @param integer $perPage
* @param integer $currentPage
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
public function paginate($collection, $perPage = 10, $currentPage = 1)
{
$offSet = ($currentPage * $perPage) - $perPage;
$otherParams = [
'path' => request()->url(),
'query' => request()->query()
];
return new LengthAwarePaginator(
$collection->forPage(Paginator::resolveCurrentPage() , $perPage),
$collection->count(),
$perPage,
Paginator::resolveCurrentPage(),
$otherParams
);
}
}
也许,这会对你有所帮助