php 如何在特定索引号的 Laravel 中将对象(模型类型对象)插入到集合对象中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27397128/
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
How to insert an object (Model type object) into Collection Object in Laravel at specific index number?
提问by Nirmalz Thapaz
I have read Dayle Rees's Code Brightto 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.
我已经阅读了 Dayle Rees 的Code Bright,以了解有关Collection
Laravel 中使用的 Eloquent 的更多信息。也做了一些其他研究,但找不到我正在寻找的答案。
I want to insert an object (Model
type object) into a Collection
Object at a specific position.
我想在特定位置将对象(Model
类型对象)插入到Collection
对象中。
For example:
例如:
This is the returned 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)
)
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:
正如您在上面看到的,[present_day]
值范围为1 to 5
,但该值3
在序列中缺失。现在,我真正想要做的是,我想明确地Attendance 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)
)
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.
注意:我不想将它转换为数组并在数组中插入。出于某种原因,我想让这个输出严格在Collection
对象中。
采纳答案by Matt Burrow
To insert an item into a collection,refer to this answer; Answer
要将项目插入到集合中,请参阅此答案;回答
Basically, splits the collection, adds the item at the relevant index.
基本上,拆分集合,在相关索引处添加项目。
You can add the item to the Eloquent\Collection
object with the add
method;
您可以Eloquent\Collection
使用该add
方法将项目添加到对象中;
$collection->add($item); // Laravel 4
or
或者
$collection->push($item); // Laravel 5
Then you can reorder the collection using the sortBy
method;
然后您可以使用该sortBy
方法对集合重新排序;
$collection = $collection->sortBy(function($model){ return $model->present_day; });
This will reorder the collection by your present_day
attribute.
这将按您的present_day
属性对集合重新排序。
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.
请注意,上述代码仅在您使用Illuminate\Database\Eloquent\Collection
. 如果您使用的是普通Eloquent\Support\Collection
,则没有add
方法。
Instead, you can use an empty array offset, the same as inserting a new element in a normal array:
相反,您可以使用空数组偏移量,与在普通数组中插入新元素相同:
$collection[] = $item;
This form also works on the Eloquent version of Collection
.
这种形式也适用于 Eloquent 版本的Collection
.
回答by stevebaros
The put method sets the given key and value in the collection:
put 方法在集合中设置给定的键和值:
$collection = collect(['product_id' => 1, 'name' => 'Desk']);
$collection->put('price', 100);
$collection->all();
// ['product_id' => 1, 'name' => 'Desk', 'price' => 100]