Laravel orderBy with column value Laravel orderBy with column value laravel laravel

Laravel orderBy with column value


You should be able to do this with an IF statement.

$programmefundings = Programmefunding::where('status', '!=', 'draft')    ->orderByRaw("FIELD(status , 'open', 'announced', 'delayed', 'closed') ASC")    ->orderByRaw("IF(status = 'announced', accouncement_date, date_start) DESC")    ->get();


I think what you are looking to do is something like this. It will orderby all items with a status of announced first by announcement date, then it will sort everything else by date_start.

$programmefundings = Programmefunding::where('status', '!=', 'draft')    ->orderByRaw("case when status = 'announced' then announcement_date end asc")    ->orderBy('date_start', 'desc')->get();