I’m having trouble ordering the birthday boys of the month on Eloquent of Laravel 5.7

Asked

Viewed 132 times

2

My query returns the birthday boys correctly, but when sorting by table column it sorts by year and by day.

This is my query

$birthday = \App\Admin\Clients::whereMonth('birthday', \Carbon\Carbon::now()->month)
                  ->orderBy('birthday', 'asc')->get();

I tried to use an array in sorting this way orderBy(['DAY', 'birthday'], 'asc'), but I can only use the column name.

I tried with WhereBetween, but unsuccessfully as well. There are other ways to do this sort of ordering?

  • birthday is what kind of value?

  • Is field DATE in the database

  • weird guy orderBy('birthday'), but, it is that you also take the random years neh? if you want to catch how, explains? because it is only filtering by month, !

  • 1

    You found the solution, but you don’t quite understand what it’s for... there’s an addendum to your answer in the comments

  • 1

    Tomorrow I will edit the answer to improve my and everyone’s understanding.

  • 1

    Read this: https://laravel.com/docs/5.7/queries#raw-methods

Show 1 more comment

1 answer

3


It’s simpler than I was imagining, just change orderBy('birthday') for:

orderByRaw('day(birthday) asc')

How claulas work with termination Raw

It does the raw query to the database, meaning you can do the same as if you were consulting without the magic methods of Eloquent, see some examples.

If you want to use a IF in the query

DB::table('clients')
          ->selectRaw('IF(name = "ana", ?, ANA)')
          ->get();

As documented in https://laravel.com/docs/5.7/queries#raw-methods

As documentation is possible to use Raw in:

  • selectRaw( )
  • whereRaw( )
  • havingRaw( ) / orHavingRaw( )
  • orderByRaw( )
  • 2

    is written in the SQL the command in the case in the clause order by, all methods that end with Raw in the Builder does this role ... I understood what you wanted

  • sort a date field will consider the full date. This includes the year as well, ordering from the oldest to the newest. The ordination you want is from the day only.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.