A
A
Annywebart2015-10-12 20:14:28
MySQL
Annywebart, 2015-10-12 20:14:28

Sorting by links with pagination in Laravel 4.2?

Please tell me how to implement sorting by links (with pagination) in Laravel 4.
I tried to do this:

$sortBy = Request::get('sortBy');
$direction = Request::get('direction');
$limit = 10;
$query = new Page;
if ($sortBy && $direction) {
                $page = Request::get('stranitsa', 1);
                if($direction == 'asc') {
                    $pages = $query->skip($limit * ($page - 1))->take($limit)->get()
                        ->sortBy(function($q) use($sortBy) {
                            return $q->$sortBy->count();
                        });
                } else {
                    $pages = $query->sortBy(function($q) use($sortBy) {
                            return $q->$sortBy->count();
                        })->reverse();
                }
                $pages = Paginator::make($pages->all(), count($pages), $limit);
        } else {
            $query = $query->orderBy('created_at', 'DESC');
            $pages = $query->paginate($limit);
        }

Everything is good until the moment when the records are placed on 1 page. As soon as there are more than 10 entries, sorting stops working, and pagination does not appear.
Please tell me what is the error or, perhaps, some better variant of sorting by links.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question