How to make an orderBy in laravel?

0

I have this orderBy , but it is not working as it should, for example:

$query = $query->where( 'approved', 1 )
               ->orderBy('featured',1)
               ->orderBy('priority',1)
               ->orderBy('created_at','desc');

I need the priority = 1 column not to overlap the column date created_at example:

If I have 2 records priority = 1 with date 16/10/2018 and priority = 0 with date 17/10/2018 , I want it to show first with today's date ( 17/10/2018 ) even being priority = 0 ie precise show priority = 1 as long as it is with date more current than priority = 0

    
asked by anonymous 17.10.2018 / 15:02

1 answer

0

First of all I've never seen a number here in orderBy('priority',1) , instead of 1 I've always used DESC or ASC . It calls the orderBy() in the order of priority, in your error in specific reverse must work:

$query = $query->where( 'approved', 1 )
               ->orderBy('featured','DESC')
               ->orderBy('created_at','DESC')
               ->orderBy('priority','DESC');

Remembering that it will still look at featured first, and you can also use it like this:

orderByRaw("featured DESC, created_at DESC, priority DESC");
    
17.10.2018 / 15:38