[12.x] Extends AsCollection to map items into objects or other values#55383
Conversation
fd91472 to
4d31559
Compare
As part of [#55383](laravel/framework#55383).
|
Does make more sense lexically to use AsCollection::of(Option::class);
AsCollection::of([Option::class, 'make']); |
AsCollection for mapAsCollection to map items into objects or other values
While I think, But you raise a good point here. Maybe, it could be something along the lines of |
What about |
To me, |
|
This is brilliant, thanks @DarkGhostHunter! I need that right now, and was making a small research before working on to for a PR. |
* Adds `AsColection::map()` section As part of [#55383](laravel/framework#55383). * Added more clarification * More clarification * Update eloquent-mutators.md Co-authored-by: Sebastian Hädrich <11225821+shaedrich@users.noreply.github.com> * Adds openinh PHP on full class * Better, almost final, clarification. * Minor change to the Option example so it makes sense * Better clarification about serialization * formatting --------- Co-authored-by: Sebastian Hädrich <11225821+shaedrich@users.noreply.github.com> Co-authored-by: Taylor Otwell <taylor@laravel.com>
What?
Rework of #55377. This extends
AsCollectionandAsEncryptedCollectionto accept a class to map each item into, or a callable as[class, method]orclass@methodnotation, without or without a custom collection class.For example, I expect the most common to be using the
map()static method to set a Class to map each item into, using the base Collection class.If the developer sets an array callable, it gets normalized into
class@methodnotation.If the developer wants to use a custom Collection class, the
using()can be used with a second parameter and either a class or array callable /class@method.Caution
Because the nature of the casts declaration, there is no support to using a Closure. The developer can use
castUsing()directly with a Closure if desired.This logic is also extended to the
AsEncryptedCollectionclass, which works in the same way.