如何在特定索引号的Laravel中将对象(模型类型对象)插入Collection对象? [英] How to insert an object (Model type object) into Collection Object in Laravel at specific index number?
问题描述
我已阅读Dayle Rees的 Code Bright ,以了解有关Laravel中使用的雄辩的Collection
的更多信息.也进行了其他一些研究,但找不到我想要的答案.
I have read Dayle Rees's Code Bright to know more about Eloquent Collection
s used in Laravel. Did some other research as well but couldn't find the answer I was looking for.
我想在特定位置的Collection
对象中插入一个对象(Model
类型的对象).
I want to insert an object (Model
type object) into a Collection
Object at a specific position.
例如:
这是返回的集合
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
.现在,我真正想做的是,我想在集合对象的[2]
索引编号/位置的位置上显式放置一个新的Attendance Object
,从而推动其余的出勤对象.我真的很努力做到这一点.我该怎么做才能使上面的集合对象看起来像下面这样:
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
,所以我不确定该怎么做.
I think there is some methods that will allow to do exactly like this if it was array. Since this is a Collection
, I am not sure how to do it.
注意:我不想将其转换为数组并在数组中进行插入.出于某种原因,我希望将此输出严格地放在Collection
对象中.
Note: I don't want to convert it this to array and do the insertion within array. For some reason, I want to have this output strictly in Collection
object.
推荐答案
要将项目插入集合中,请参考此答案; 答案
To insert an item into a collection,refer to this answer; Answer
基本上,拆分集合,然后在相关索引处添加项目.
Basically, splits the collection, adds the item at the relevant index.
您可以使用add
方法将该项目添加到Eloquent\Collection
对象中;
You can add the item to the Eloquent\Collection
object with the add
method;
$collection->add($item); // Laravel 4
或
$collection->push($item); // Laravel 5
然后,您可以使用sortBy
方法对集合进行重新排序;
Then you can reorder the collection using the sortBy
method;
$collection = $collection->sortBy(function($model){ return $model->present_day; });
这将通过您的present_day
属性对集合进行重新排序.
This will reorder the collection by your present_day
attribute.
请注意,以上代码仅在使用Illuminate\Database\Eloquent\Collection
时有效.如果您使用的是普通Eloquent\Support\Collection
,则没有add
方法.
Note that the above code will only work if you are using Illuminate\Database\Eloquent\Collection
. If you are using a plain Eloquent\Support\Collection
, there is no add
method.
相反,您可以使用空数组偏移量,就像在普通数组中插入新元素一样:
Instead, you can use an empty array offset, the same as inserting a new element in a normal array:
$collection[] = $item;
此表格也适用于Collection
的口才版本.
This form also works on the Eloquent version of Collection
.
这篇关于如何在特定索引号的Laravel中将对象(模型类型对象)插入Collection对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!