Laravel Query Builder Distinct

This is a shoutout to Laravel’s Query Builder method distinct(). I feel sad and embarrassed that I didn’t know about it sooner. There are a lot of instances where I need to get a column’s unique values. And instead of using groupBy() and wasting a lot of resources.

Model::select()->where()->groupBy()->get() //etc....

You can use distinct(), which will search for distinct instances instead of retrieving each one and grouping them.

Model::select()->where()->distinct()->get() //etc..

This saves a significant amount of time and memory.

Just thought distinct() deserves it’s own post.

Good luck!

Leave a Comment