我阅读了Dayle Rees的Code Bright篇文章,以了解更多关于拉威尔中使用的有说服力的Collection
篇文章.我也做了一些其他的研究,但没有找到我想要的答案.
I want to insert an object (Model
type object) into a Collection
Object at a specific position.
For example:
这是退回的藏品
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)
)
As you can see above [present_day]
have a values ranging from 1 to 5
, but the value, 3
is missing in the sequence. Now, what I really want to do is, I want to explicitly put a new Attendance Object
at the Collection Object's position of [2]
index number/position, thus by pushing the rest of the Attendance Object. I am really struggling to do this right. How can I do this to make above collection object to look like something as below:
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
,我不知道该怎么做.
Note:我不想把它转换成数组,在数组中插入.出于某种原因,我想把这个输出严格地放在Collection
个对象中.