I have this Laravel collection with some fields:
Collection {#324 ▼
#items: array:3 [▼
0 => array:5 [▼
"img_src" => "http://newregalospersonales.dev/./svg/regalos-personalizados-gift.svg"
"img_alt" => "<span>Regalos</span> originales"
"class" => "personalised-gifts"
"elements" => array:4 [▼
0 => array:2 [▶]//element1
1 => array:2 [▶]//element2
2 => array:2 [▶]//element3
3 => array:2 [▶]//element4
]
]
1 => array:5 [▼
"img_src" => "http://newregalospersonales.dev/./svg/love-gifts.svg"
"img_alt" => "<span>Regalos</span> románticos"
"class" => "love-gifts"
"elements" => array:4 [▼
0 => array:2 [▶]//element1
1 => array:2 [▶]//element2
2 => array:2 [▶]//element3
3 => array:2 [▶]//element4
]
]
2 => array:5 [▼
"img_src" => "http://newregalospersonales.dev/./svg/regalos-ocasiones-especiales.svg"
"img_alt" => "<span>Detalles</span><br>de boda"
"class" => "wedding"
"elements" => array:4 [▼
0 => array:2 [▶]//element1
1 => array:2 [▶]//element2
2 => array:2 [▶]//element3
3 => array:2 [▶]//element4
]
]
]
}
And I only get the collection with the field 'elements'.
I want an result like this:
Collection {#324 ▼
#items: array:3 [▼
0 => array:4 [▼
0 => array:2 [▶] //element1
1 => array:2 [▶ //element2
2 => array:2 [▶] //element3
3 => array:2 [▶] //element4
]
1 => array:4 [▼
0 => array:2 [▶] //element1
1 => array:2 [▶] //element2
2 => array:2 [▶] //element3
3 => array:2 [▶] //element4
]
2 => array:4 [▼
0 => array:2 [▶] //element1
1 => array:2 [▶] //element2
2 => array:2 [▶] //element3
3 => array:2 [▶] //element4
]
]
}
Is there any way to do this? I´m using Laravel 5.3 version and I not have idea about that collection filter use for this case.
You can use pluck()
method:
$plucked = $collection->pluck('elements');
The pluck method retrieves all of the values for a given key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With