I recently submitted a pull request to the Laravel framework to add a new Eloquent attribute called CollectedBy. In short, it allows you to use an attribute to configure your Eloquent model to use a specific Collection class, rather than the default (which is Illuminate\Database\Eloquent\Collection).

Just to mention it: the concept of using custom collections is nothing new. You've been able to do that for a while by overriding the model's newCollection method. All the attribute does is let you configure the collection class to use in a more declarative way, but hopefully this may make it a bit more approachable.

As the PR was merged and I saw some questions popping up over at X, I figured I should write a few lines about the benefits (and drawbacks!) of using custom collections.

continue reading on alsterholm.com

⚠️ This post links to an external website. ⚠️