-
I have an idea to introduce type specific collections in Laravel. This is a simple idea that I feel can really have a lot of applications, but since I haven't contributed before I'd love to get some feedback first 🙏. Essentially, I'm looking to introduce a new One very simple use case to demonstrate this is to consider a collection of enums:
Does this contain only the enum values (strings / integers etc), or is an instantiated enum object? This would act similar to how we can extend Eloquent collections for models, except it would actually validate that the items adhere to a class type. The class that I'm proposing would look something like this:
Once you then fit this to your custom class, it will allow us to build strongly typed collections that can be passed as function arguments. You always know what you are working with, and even simpler to review. There are various enum packages that we use together with Laravel as well, and we can help support those packages with a dedicated enum collection as well:
This could even potentially be used with Laravel's model attribute casting (but that's possible future implementations that extend beyond what I'm currently proposing). I know this may seem rather simple, but I'd love to hear some of your opinions, improvements, or if this even makes sense at all to add. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
There are a lot of packages doing that. Including mine. https://packagist.org/packages/henzeb/laravel-typed-collection Oh, and someone is making steps with it. Currently it's possible to use 'ensure' , which throws an exception if types does not match. |
Beta Was this translation helpful? Give feedback.
There are a lot of packages doing that. Including mine. https://packagist.org/packages/henzeb/laravel-typed-collection
Oh, and someone is making steps with it. Currently it's possible to use 'ensure' , which throws an exception if types does not match.