Skip to content

[5.4] Add orderByDesc() to Query Builder - #18292

Merged
taylorotwell merged 1 commit into
laravel:5.4from
KennedyTedesco:builder-5.4
Mar 10, 2017
Merged

taylorotwell merged 1 commit into
laravel:5.4from
KennedyTedesco:builder-5.4

Conversation

@KennedyTedesco

Copy link
Copy Markdown
Contributor

Just a shortcut of something that I use a lot.

@GrahamCampbell

Copy link
Copy Markdown
Collaborator

👍

@taylorotwell
taylorotwell merged commit 4483e70 into laravel:5.4 Mar 10, 2017
@KennedyTedesco
KennedyTedesco deleted the builder-5.4 branch March 10, 2017 14:35
@joshmanders

Copy link
Copy Markdown
Contributor

So I'm on the fence about this... If we introduce orderByDesc($name) then shouldn't we have orderByAsc($name) too? Other wise inconsistencies...

Then I think, maybe a magic method, that does like orderByName('desc') and Name is column name, sorta like magic method for whereName('whatever').

Thoughts?

@KennedyTedesco

KennedyTedesco commented Mar 10, 2017 •

Copy link
Copy Markdown
Contributor Author

I don't think so. asc it's already implicit (see orderBy) and, after all, this is the default of MySQL (which is used by the majority of the devs):

The default sort order is ascending, with smallest values first. To sort in reverse (descending) order, add the DESC keyword. https://dev.mysql.com/doc/refman/5.7/en/sorting-rows.html

@stefensuhat

stefensuhat commented Mar 22, 2017 •

Copy link
Copy Markdown

actually Laravel already provide latest and oldest. you can pass the argument column name. ex: latest('posted_at')

@aliusa

aliusa commented Mar 23, 2017 •

Copy link
Copy Markdown

orderByDesc() is more rememberable than latest()...

public function orderByDesc($column)
{
$this->orderBy($column, 'desc');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use

    public function orderByDesc($column)
    {
        return $this->orderBy($column, 'desc');
    }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you go @CupOfTea696 #18465

Thanks. 😄

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would be perfect

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

salmanarshad321 pushed a commit to salmanarshad321/framework that referenced this pull request Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants