I’m trying to sort nested collection but i can’t get correct results.
category tags collection
[
{
"id": 1,
"name": "Age",
"tags": [
{
"id": 1,
"name": "18+"
},
{
"id": 2,
"name": "21+"
},
{
"id": 3,
"name": "25+"
},
{
"id": 4,
"name": "30+"
},
{
"id": 5,
"name": "17+"
},
{
"id": 6,
"name": "16+"
},
{
"id": 7,
"name": "26+"
},
{
"id": 8,
"name": "13+"
},
{
"id": 9,
"name": "24+"
},
{
"id": 10,
"name": "20+"
},
{
"id": 11,
"name": "19+"
}
],
}
]
i’m trying to sort tags:
<?php
$categoryTagsSorted = $categoryTags->transform(
function ($categoryTag) {
$sortedTags = $categoryTag->tags->sortBy('name');
// $sortedTags->values() return correct sorted array
$categoryTag->tags = $sortedTags->values();// < - this not work
return $categoryTag;
}
);
$categoryTagsSorted
returns untouched results, but when i assign $sortedTags
to $categoryTag->newField = $sortedTags->values()
i’ve got sorted tags in newField
.
Question is how to return collection with sorted tags?
2
Answers
In your code I believe you have misunderstood the use of the
transform()
method. It is used to iterate over each item in the collection and replaces the item with the value you give it in the callback. In your code you have also simply returned the original value without applying any ‘transformation’ to it. If you wish to alter the value of tags on the object you could try reassigning the object property:Read the docs on the
transform
method here: https://laravel.com/docs/9.x/collections#method-transform#Update
I think you can simply use
map
and re-assign the tags property to accomplish the solution:The following test code worked OK for me…
Output:
Try it out here: https://web.tinkerwell.app/#/snippets/e2c93c20-5706-4ffd-bcc0-601aae861c8b (takes a min to load)
you can try this :