mardi 7 mai 2019

laravel eloquent collection orderby firstly null then filed value

I have $items with this Illuminate\Database\Eloquent\Collection instance. The result of dd($items->toArray()) is

array:3 [▼
  0 => array:3 [▼
    "id" => 10
    "start_date" => "Jan 1997"
    "end_date" => "Jan 2000"
  ]
  1 => array:3 [▼
    "id" => 9
    "start_date" => "Jan 2000"
    "end_date" => "Jan 2003"
  ]
  2 => array:3 [▼
    "id" => 6
    "start_date" => "Jan 2007"
    "end_date" => ""
  ]
]

I want to order by end_date. Firstly empty values than desc like this

array:3 [▼
  0 => array:3 [▼
    "id" => 6
    "start_date" => "Jan 2007"
    "end_date" => ""
  ]
  1 => array:3 [▼
    "id" => 9
    "start_date" => "Jan 2000"
    "end_date" => "Jan 2003"
  ]
  2 => array:3 [▼
    "id" => 10
    "start_date" => "Jan 1997"
    "end_date" => "Jan 2000"
  ]
]

How can do it in correct way. I try $items->sortByDesc('end_date') but that case empty values is the end



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire