In this episode, we dive into how to alias aggregate columns in Laravel. We've already seen how Laravel gives us a default property name (like articles_count
) when using aggregates, but sometimes you want to change this to something else that's more meaningful for your use case.
We walk through how easy it is to provide your own alias—just use the as
keyword when specifying the aggregate in your query (for example, articles as articles_count_alias
). Laravel will automatically update your query to use that alias, and when you access the property in your model, it'll have the name you specified.
Throughout this video, you'll see a live demo of updating the alias and confirming that it shows up as expected on both your models and in the SQL query itself. It's a handy feature, and you'll want to remember it as you add more aggregates to your queries. Alias away!