Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

saimiris's avatar

Column sorting with pagination on Laravel 5.1

Hi there,

What is the simplest and fastest way to get links on the top of sorting paginated columns lists? I tried this package https://github.com/Kyslik/column-sortable that seemt good but I got this error https://github.com/Kyslik/column-sortable/issues/7

I need the solution to support pages number on sorting links, to support sorting criteria on pagination links and to support join queries.

Thanks!

0 likes
30 replies
jlrdw's avatar

Edited, sorry I was surprised someone using or ready for laravel did not understand how to sort a column.

You're kidding you simply make one of the headers as a link whip back to the controller let the controller do its thing to sort by that column. This is first or second grade programming.

saimiris's avatar

Hi,

Many thanks for your answer. Please do not get upset, I am not a computer science beginner but it is nowhere said in the Laravel documentation that a sorting link would be automatically processed by the Laravel controller. There is also no doc about the exact syntax of such a sorting link. If you have a code example to provide, I am very interested!

Apart from that, if it is as simple as you said, I an wondering why someone spent time writing this dedicated component: https://github.com/Kyslik/column-sortable

Best,

5 likes
jlrdw's avatar

Edited, sorry I was surprised someone using or ready for laravel did not understand how to sort a column.

This is basic PHP and HTML has nothing at all to do with laravel, why would there be any documentation on this. Surely you're accomplished in PHP and HTML and some basic JavaScript before delving into Laravel correct? A basic if statement in your controller would handle what you need to do with the correct code to order by the column you need. Or possibly a switch statement. https://www.google.com/search?q=php+make+colum+link+to+sort&oq=php+make+colum+link+to+sort&aqs=chrome..69i57.19150j0j4&client=tablet-android-google&sourceid=chrome-mobile&ie=UTF-8#safe=active&q=php+link+for+column+sort

opheliadesign's avatar

Yikes these were some pretty harsh replies. I really fail to see how questioning someone's abilities or implying that they should know how to do what they're asking for help with benefits anyone.

102 likes
jekinney's avatar

On a paginated query you only get the number of rows that you specify on each page. So as stated in order to sort each table column again with PHP you have to perform another query with an order by added to it. Return and display that as a paginated list on your table. If you try and sort the original query you'll only sort the few rows you have displayed and not every row.

If it's a small number like a hundred or so total rows maybe you can utilize data tables a jQuery plugin. Pretty easy to use and does all the work for you. You can use it for large amounts of data but in order for it to work well you have a lot more set up. Ie sever side.

jekinney's avatar

@opheliadesign yeah, a lot of basic questions end up here that google can answer. I tend just not answer.... Even though harsh and probably not needed, it's true. Seems any framework more so, has dev's hacking out sites but expect a CMS and don't understand basic functionality of the core language. Thus ask the most basic questions because of a possible lack of knowledge that is arguably expected.

Bottom line though we all learn differently.

jlrdw's avatar

Edited, sorry I was surprised someone using or ready for laravel did not understand how to sort a column.

But really I did not mean to be rude but let's face it it would be like me applying for a job as a mathematician professor at a university and then going on a forum and asking how do you add two plus two I feel anyone should know some basics before messing around with Laravel that is the biggest single problem people delve into Laravel and don't even know the basic core language. PHP.

jekinney's avatar

I deal with clients who claim to know html and CSS. Yet when I explain something I see their eyes glaze over. Come to find out they really can use WordPress, install themes and plug-ins. Par for the course, but is frustrating. Want to throat chop them at times. Especially when they try to keep it going.

Had a client want to make the registration page form fields dynamic just the other day, just in case....

saimiris's avatar

Thanks for your answers guys. I am still very surprised and a bit upset that Laravel does not include a simple helper to create sort links (like in CakePHP does -> http://book.cakephp.org/3.0/en/views/helpers/paginator.html#creating-sort-links ). This is what I was looking for, and believe me I know a lot of HTML, PHP, CSS and jQuery.

@jlrdw -> I really think you should calm down and relax instead of getting upset answering questions you consider too stupid to be asked.

7 likes
Prullenbak's avatar

Some harsh replies indeed. Sure some knowlegde of PHP is expected before you just ask around on a forum like this, but the least you/we can do is point people in the right direction, right? Just link them a video here on Laracasts, or some other tutorial. No need to be condescending.

13 likes
jwindhorst's avatar

I'm glad that the issue appears to be fixed. I also 100% agree that these are some pretty harsh responses, and IMHO not even accurate. For all the insults about this being a php/db issue and NOT a Laravel issue no one bothered to mention the "Appending To Pagination Links" (http://laravel.com/docs/5.1/pagination#paginating-eloquent-results%29) functionality of the paginator class. So hopefully the next person that runs into this problem will at least know what to look for on that page.

7 likes
jlrdw's avatar

Edited, sorry I was surprised someone using or ready for laravel did not understand how to sort a column.

@jwindhorst that was two months ago I've toned down and I'm nicer now.

KingOtar's avatar

jlrdw Comments were a little harsh yeah. You don't need to use basic php and html even though you could if you wanted to. Laravel has tools built in to help us with that, and that's why we are using laravel. Also saying "Yikes, this is basic stuff" is not that helpful.

There are other good responses here. Listen to them.

3 likes
spuds10's avatar

Hey have you checked out this https://laracasts.com/lessons/sorting-tabular-data? Jeffrey explains this really well. As far as keeping the sorted table when changing pages, you can do this in your blade view.


//Paginated links
{{ $collection->appends(['query-param' => 'value'])->render() }}

I know this was a couple months ago but hope this helps somebody. This laracast sure helped me.

1 like
kyslik's avatar

@spuds10 lesson in your link is targeted mostly for Laravel 4.0, do you still code in L4 (even brand new projects)? just curious.

1 like
abellowins's avatar

Controller

    use Illuminate\Support\Facades\Input;

    public function index(JobListing $listings)
    {

        $sort_by = Input::get('sortBy');
        $direction = Input::get('direction');

        $openings = $listings->getPaginated([
            'sortBy' => $sort_by, 
            'dir' => $direction
        ]);

        return view('backend.openings.index', [
            'openings' => $openings
        ]);
    }

Helper

use Illuminate\Support\Facades\Input;

function sortOpeningsBy($column)
{
    $dir = (Input::get('direction') == 'asc') ? 'desc' : 'asc';

    return route('admin.openings.index', [
        'sortBy' => $column,
        'direction' => $dir
    ]);

}

Model

    public function getPaginated(array $params)
    {

        if($this->isSortable($params)){
            return $this->orderBy($params['sortBy'], $params['dir'])->paginate(25);
        }

        return $this->paginate(25);

    }
    protected function isSortable(array $params)
    {
        return $params['sortBy'] and $params['dir'];
    }

Partial View

Title <a href="{!! sortOpeningsBy('title') !!}"><small></a>
1 like
spuds10's avatar

@kyslik Ha no I just got into Laravel when laravel 5 came out. Though that video was in 4 in was still super useful for 5.

johnsonson's avatar

@jlrdw wow aren't you a cock knocker.. I just ended here looking for helper functions for sorting a paginated table and I'm greeted with assholish replies coming from picture of dude with a child rapist mustache. neat place that this has been sitting up here for a year+ Who would want to join a non-moderated community other than to bash this cunt.

3 likes
jlrdw's avatar

@johnsonson know take a breath, calm down, and start learning laravel. It's a dam good framework. I haven't raved like that in a long time.

jordano1's avatar

jlrdw seems like he's got a rod up his ass

1 like
Snapey's avatar

OK guys just leave it there... Perhaps @JeffreyWay would consider taking this entire post down for the sanity of the community.

Its not providing anything of value.

If this is not taken down, perhaps people new to the post might consider this pair of videos from forum contributor @SaeedPrez https://youtu.be/rpIWL4MPhkE

2 likes
MrMoto9000's avatar

What would help is if @jlrdw deleted his obnoxious and unhelpful answers. (BTW - There's no apology in this thread, jlrdw.)

To make matters worse jlrdw's answers aren't merely obnoxious and unhelpful, they're absolutely wrong, too.

jlrdw's avatar

Edited, sorry I was surprised someone using or ready for laravel did not understand how to sort a column.

@JohnnyW2001 it gets back to the electrician thing:

I will hire the electrician who learned the basics correctly and earned their journeymans license.

You can hire the beginner

I still stand by this

that is the biggest single problem people delve into Laravel and don't even know the basic core language. PHP.

And of course some html.

Column sorting is still just basic logic in a controller.

When people come to the laravel forum, and in my opinion, they should already be well versed in php, html, css, and some js. (the js maybe, maybe not).

Someone should not delve into a framework like laravel with zero knowledge.

Now I am sorry that is just some (basic php / html / controller if construct) to sort a column. I am not sorry I pointed that out.

Yes there are packages that do that, and if OP wants to use a package it's not my concern.

My shock was the fact someone was actually using laravel and did not know how to sort a column of data.

Again that would be like a math professor asking how to add 3+2+5.

And may thanks for bringing up old junk.

they're absolutely wrong, too.

So a useful link was wrong

http://stackoverflow.com/questions/22114655/sort-a-database-with-a-href-in-html-and-php.

Well I will not call you a liar. Let's just say you did not tell the truth.

All my replies are edited.

kyslik's avatar

@jlrdw I made the package so I can use it in my projects. The idea is from http://hack.swic.name/laravel-column-sorting-made-easy/ I added something of mine (font-awesome and some small tweaks) and published it as a configurable package.

I know sorting is trivial, but its way nicer to have controller 5 lines long instead of 20 + solving all the edge cases. Do not forget the re-inventing the wheel problem.

Saneesh's avatar

@jlrdw After two years also if you are getting negative comments for each of your reply, then there should be something need to be corrected from your side. People are finding time to reply ONLY to you like me. Frameworks are help the developers to solve the problems easily. So everyone in the forums are seeking for the easy solutions otherwise there is not need to use frameworks, they can write their code in core PHP.

jlrdw's avatar

Frameworks are help the developers to solve the problems easily

Yes and with some eloquent queries someone could implement column sorting.

Laravel doesn't have it out of the box.

So someone would have to use a package or implement themselves.

To re-quote

Frameworks are help the developers to solve the problems easily

Laravel has a learning curve.

If it was so easy would there be so many questions on the forum.

I still stand by my original answer that a developer should know how to implement this stuff on their own without a package.

you are free to do whatever you so desire like use a package if you wish.

After two years also if you are getting negative comments for each of your reply

I don't I have many best answer's, and plenty have thanked me for helping them.

Please or to participate in this conversation.