Laravel 8 prepend() {#collection-method}
2021-07-19 09:44 更新
prepend
方法将指定的值添加到集合的开头:
$collection = collect([1, 2, 3, 4, 5]);
$collection->prepend(0);
$collection->all();
// [0, 1, 2, 3, 4, 5]
你也可以传递第二个参数来设置新增加元素的键:
$collection = collect(['one' => 1, 'two' => 2]);
$collection->prepend(0, 'zero');
$collection->all();
// ['zero' => 0, 'one' => 1, 'two' => 2]
以上内容是否对您有帮助:
更多建议: