Just see how others have "worked up" a query, such as
https://laracasts.com/discuss/channels/eloquent/orderby-computed-related-attribute
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Is there a way to group by a calculated property?
So for example, if you wanted to group all of your users by age and get a count of how many were in each year, or age bracket, how could you do it? I could store the user's DoB in the database and then use getAgeAttribute function on the user model to return the age of that user via $user->age
In this particular example I can do it as a raw sql, but I was wondering if there was a more Laravel way of doing it for this example or better a more generic way of doing it when dealing with items properties that need to be calculated, but are not dates?
Just see how others have "worked up" a query, such as
https://laracasts.com/discuss/channels/eloquent/orderby-computed-related-attribute
Please or to participate in this conversation.