Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel collection return collection with only field

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.

like image 933
RichardMiracles Avatar asked Jan 05 '23 22:01

RichardMiracles


1 Answers

You can use pluck() method:

$plucked = $collection->pluck('elements');

The pluck method retrieves all of the values for a given key.

like image 161
Amit Gupta Avatar answered Jan 13 '23 10:01

Amit Gupta