Laravel remove first item in collection

I want to remove the first item in a collection:

unset($productData->images->first())

      

The above doesn't work.

I want it to be so that later when I do foreach, the first element does not appear:

@foreach($productData->images as $images)
    <img class="product-thumb" src="/images/products/{{$images->src_thumb}}">
@endforeach

      

How can I do that?

+3


source to share


3 answers


You can use shift()

to get and remove the first item of Laravel collection.
See Illumination Source \ Support \ Collection

And here's an example:



$productData->images->shift();

      

+5


source


I am doing:



$first_key = $collection->keys()->first();
$collection = $collection->forget($first_key);

      

0


source


In Laravel 5, you can use the slice () method :

$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
$slice = $collection->slice(4); 

$slice->all();    // [5, 6, 7, 8, 9, 10]

      

0


source







All Articles