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?
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.
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.
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();
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