Laravel 删除集合中的第一项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27201468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel remove first item in a collection
提问by imperium2335
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 时,第一项不会出现:
@foreach($productData->images as $images)
<img class="product-thumb" src="/images/products/{{$images->src_thumb}}">
@endforeach
How can this be done?
如何才能做到这一点?
回答by lukasgeiter
You can use shift()
to get and remove the first item of a Laravel collection.
See the Source of Illuminate\Support\Collection
您可以使用shift()
获取和删除 Laravel 集合的第一项。
查看Illuminate\Support\Collection的来源
And here's an example:
这是一个例子:
$productData->images->shift();
回答by Kostiantyn
I do:
我愿意:
$first_key = $collection->keys()->first();
$collection = $collection->forget($first_key);