New to Eloquent and having a puzzling issue with regards to a return value from a method. The when() method works as expected for two of three almost identical filters, but the third one returns a results set that is formatted differently.
I am able to filter a collection of results into two separate arrays of objects using the when() method. But a third, identical filter ($pastBooks) used on the same collection returns an indexed object instead of an array of objects. All data within the database is standardized and uses the same carbon date format. I'm stumped, and would be grateful for any assistance.
$bookings = Booking::leftJoin('classes', 'bookings.class_id', '=', 'classes.class_id')
->leftJoin('users', 'bookings.user_id', '=', 'users.id')
->get(['bookings.booking_id', 'users.name','classes.class_id',
'classes.class_name', 'classes.class_date', 'users.phone','users.email']);
$todayBook = $bookings->where('class_date', \Carbon\Carbon::today());
$thisWeekBooks = $bookings->where('class_date', '>', \Carbon\Carbon::today());
$pastBooks = $bookings->where('class_date', '<', \Carbon\Carbon::today());
return(['today' => $todayBook, 'past' => $pastBooks,'thisWeekBooks' => $thisWeek]);
//Results
{
today: [ ],
past: {
11: {
booking_id: 220,
name: "trial",
class_id: 8,
class_name: "Intermediate",
class_date: "2020-11-30 21:47:23",
phone: 12356789,
email: "trial@gmail.com"
},
12: {
booking_id: 212,
name: "trial",
class_id: 9,
class_name: "one-to-one",
class_date: "2020-12-30 21:47:23",
phone: 12356789,
email: "trial@gmail.com"
},
13: {
booking_id: 213,
name: "trial",
class_id: 9,
class_name: "one-to-one",
class_date: "2020-12-30 21:47:23",
phone: 12356789,
email: "trial@gmail.com"
}
},
thisWeek: [
{
booking_id: 207,
name: "trial",
class_id: 7,
class_name: "beginner",
class_date: "2021-02-17 21:47:23",
phone: 12356789,
email: "trial@gmail.com"
},
{
booking_id: 208,
name: "trial",
class_id: 7,
class_name: "beginner",
class_date: "2021-02-17 21:47:23",
phone: 12356789,
email: "trial@gmail.com"
}
]
}