我读过Dayle Rees的 代码明亮 了解更多有关Eloquent的信息 Collection
用于Laravel。做了一些其他的研究,但找不到我想要的答案。
我想插入一个对象(Model
类型对象)成一个 Collection
对象在特定位置。
例如:
这是返回的集合
Illuminate\Database\Eloquent\Collection Object
(
[0] => Attendance Object
([present_day] => 1)
[1] => Attendance Object
([present_day] => 2)
[2] => Attendance Object
([present_day] => 4)
[3] => Attendance Object
([present_day] => 5)
)
如上所示 [present_day]
有一个值范围从 1 to 5
,但价值, 3
在序列中缺失。现在,我真正想做的是,我想明确地提出一个新的 Attendance Object
在Collection Object的位置 [2]
索引号/位置,因此通过推送出席对象的其余部分。我真的很难做到这一点。我怎么能这样做使上面的集合对象看起来像下面的东西:
Illuminate\Database\Eloquent\Collection Object
(
[0] => Attendance Object
([present_day] => 1)
[1] => Attendance Object
([present_day] => 2)
[2] => Attendance Object // This is where new object was added.
([present_day] => 3)
[4] => Attendance Object
([present_day] => 4)
[5] => Attendance Object
([present_day] => 5)
)
我认为如果是数组,有一些方法可以完全像这样做。因为这是一个 Collection
,我不知道该怎么做。
注意: 我不想将它转换为数组并在数组中插入。出于某种原因,我希望严格遵守此输出 Collection
目的。