-
Notifications
You must be signed in to change notification settings - Fork 11.5k
[8.x] Add pack and packAll to Collection #32498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
yes, this is pretty cool @sileence - thus, we do not have to be calling |
Please rebase your PR. |
b278ede
to
2b99209
Compare
Ping @JosephSilber |
These Also, If we are to do something like this, I think it would make more sense to have a generic method that just recursively converts each array into a collection. I don't have a good name off the top of my head, but something like this: $data = $collection->toDeepCollection(); Then you can do the whitelisting in your own code: $data->only(...); or $data->map->only(...); To me, such a method would have broader appeal, and is not specific to the given context @sileence is currently working in (validated data). |
@JosephSilber I thought about using I don't think we should convert all the arrays recursively to collections because there are times where it's useful to store an array of data. For example
A recursive method will break that. The most common scenario that I've had to deal with over an over is sending a simple list of data, like a list of orders, items, users, etc. |
If we were to add such a method, it would take a number determining how many levels deep you want to convert, and would default to INF (just like flatten).
|
It would be also cool to have a limit on how deep you want the collection: $data = $collection->toDeepCollection(2); |
@JosephSilber I'm thinking:
But right now Is there any reason why |
https://laravel.com/docs/7.x/collections#method-collect
|
Thanks for your pull request to Laravel! Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include. If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions! If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response. |
Allow a developer to get a sub-collection from an array of arrays:
Following #32379
This PR add two methods that should be very useful when dealing with nested validated data.
For example:
Assuming input looks like this:
and validation looks like this:
$data
will contain aValidated
object that extendsCollection
but if we callget
like this:$settings = $data->get('settings')
then$settings
will be a plain array that cannot be passed to a model without$fillable
.But calling
$data->pack('settings')
will "pack" the underlying array as another instance ofValidated
that can be passed to aSetting
model without fillable.Furthermore, calling
$data->pack('settings', ['registrations', '...']);
will pack a new collection with whitelisted keys only.Calling
$data->packAll('users')
will get aValidated
collection containingValidated
collections with users data.Calling
$data->packAll('users', ['name', 'email', 'password'])
will get aValidated
collection containingValidated
collections of user data with those 3 attributes only.The developer can choose whether to add more strict validation with:
'users.*' => ['required', 'array:name,email,password'],
(#32452)or selecting specific keys just before "packing" the data to pass it to an Eloquent model or even to different Eloquent models!