Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mcadio's avatar

Sorting a collection by appt_time using carbon field

I have a collection of appointments. Unbooked, and booked. I cycled through the unbooked, and pushed them into the booked collection and want to sort them all by time so I can see my schedule in order by time. I can handle the a different display for each type in the view. My problem is they are not sorting, and they are all carbon times for the same date.

public function getApptBookButtons($t, $oh, $date) {
        foreach($t as $type => $times) {
            foreach($times as $time) {
                $slot = new Appt([
                'appt_carbon' => new Carbon($date->format('Y-m-d').' '.$time->format('H:i:s')),
                'appt_date' => $date->format('Y-m-d'),
                'appt_time' => new Carbon($date->format('Y-m-d').' '.$time->format('H:i:s')),
                'appt_type_id' => $type,
                'doctor_id' => $oh->doctor_id,
                'officehour_id' => $oh->id,
                'appt_note' => $type !== 2 ? '<br /> NEW PATIENT' : null,
                ]);
//                dd($slot);
                $oh->appts->push($slot);
            }
        }

        $oh->appts = $oh->appts->sortBy('appt_carbon');

I did a data dump and its a collection of Appt models. They are not sorting. The pushed appointments stay at the end of the list. Why can't I get this?

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It seems like you're trying to sort a collection of Appt models by the appt_carbon attribute, which is a Carbon instance. The sortBy method should work correctly for this purpose, assuming that appt_carbon is a date or datetime field. However, if the appointments are not sorting as expected, there might be an issue with the collection being modified after the sort operation or the sort operation not being applied correctly.

Here's a revised version of your code that ensures the sorted collection is reassigned back to $oh->appts:

public function getApptBookButtons($t, $oh, $date) {
    foreach($t as $type => $times) {
        foreach($times as $time) {
            $slot = new Appt([
                'appt_carbon' => new Carbon($date->format('Y-m-d').' '.$time->format('H:i:s')),
                'appt_date' => $date->format('Y-m-d'),
                'appt_time' => new Carbon($date->format('Y-m-d').' '.$time->format('H:i:s')),
                'appt_type_id' => $type,
                'doctor_id' => $oh->doctor_id,
                'officehour_id' => $oh->id,
                'appt_note' => $type !== 2 ? '<br /> NEW PATIENT' : null,
            ]);
            // Push the new slot into the appointments collection
            $oh->appts->push($slot);
        }
    }

    // Sort the appointments collection by the 'appt_carbon' field
    $sortedAppts = $oh->appts->sortBy(function ($appt) {
        return $appt->appt_carbon;
    });

    // Reassign the sorted collection back to $oh->appts
    $oh->appts = $sortedAppts->values(); // Calling values() to reset the keys after sorting
}

A few things to note:

  1. The sortBy method returns a new sorted collection, so you need to reassign it back to $oh->appts.
  2. After sorting, it's a good practice to call values() on the collection to reset the keys. This ensures that the collection is reindexed and can prevent issues when converting the collection to JSON or when iterating over it.
  3. Ensure that the appt_carbon field is indeed a Carbon instance and that it's formatted correctly for comparison.

If you're still facing issues, you might want to double-check the contents of the appt_carbon field to ensure that they are valid Carbon instances and that they are comparable. You can also add some logging or dd() statements to inspect the collection before and after sorting to verify that the sorting is being applied as expected.

1 like
mcadio's avatar

Oh my goodness! Thank you for explaining this so clearly. I tried doing the assigning before I posted and using values, but I guess I needed to sort using the function rather than just $oh->appts->sortBy('appt_carbon')

It's finally in order!

Please or to participate in this conversation.