Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 如何在sortBy和where运算符获取的集合中使用分页?_Sorting_Filter_Pagination_Laravel 5.5 - Fatal编程技术网

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
        );
    }
}
也许,这会对你有所帮助