Following up on my previous article on https://chasingcode.dev/blog/mysql-cte-group-by-year-month-with-gaps/, I am offering an implementation of the same technique in Laravel. Note This requires MySQL v8.0+.

There's a Laravel package for everything, and CTEs are no exception.

Here's how this looks in Laravel: $user_id = 1; $bike_id = 100; $bindings = [$user_id, $bike_id, $user_id, $bike_id, $user_id, $bike_id]; $query = " date ) AS ( DATE(LAST_DAY(MIN(start_date))) rides user_id = ?

I hope Laravel will have first class support for Common Table Expressions at some point in the future, but for now these two techniques should suffice.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners