Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel remove first item in a collection

Tags:

I want to remove the first item in a collection:

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

The above doesn't work.

The reason I want this is so that later when I do a foreach, the first item doesn't come out:

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

How can this be done?

like image 934
imperium2335 Avatar asked Nov 29 '14 10:11

imperium2335


People also ask

How do you pluck in Laravel?

The Laravel pluck () as the name suggests, extracts certain values, as suggested. It literally plucks the information out from the given array and produces it as an output. However, it is most effective against objectives, but will work well against arrays too.

What is first () in Laravel?

The Laravel Eloquent first() method will help us to return the first record found from the database while the Laravel Eloquent firstOrFail() will abort if no record is found in your query. So if you need to abort the process if no record is found you need the firstOrFail() method on Laravel Eloquent.


1 Answers

You can use shift() to get and remove the first item of a Laravel collection.
See the Source of Illuminate\Support\Collection

And here's an example:

$productData->images->shift(); 
like image 162
lukasgeiter Avatar answered Oct 22 '22 07:10

lukasgeiter