Laravel Eloquent 使用时间戳一次插入 3 行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41366330/
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
Laravel Eloquent insert 3 rows at once with timestamp
提问by Benyi
I'm making a site with Laravel 5.2
我正在使用 Laravel 5.2 制作网站
I would like to do is
我想做的是
INSERT
3 rows at the same time- The new 3 rows have to contain timestamp
created_at
updated_at
.
INSERT
3行同时- 新的 3 行必须包含时间戳
created_at
updated_at
。
With the Query Builder
method insert
, Yes, it does insert 3 rows at once by passing an array like:
使用该Query Builder
方法insert
,是的,它通过传递如下数组一次插入 3 行:
GroupRelation::insert([
[
'application_id' => $applications[0]->a_id,
'group_id' => $group->id,
],
[
'application_id' => $applications[1]->a_id,
'group_id' => $group->id,
],
[
'application_id' => $applications[2]->a_id,
'group_id' => $group->id,
],
]);
The code above works fine. But this cannot touch the timestamp. Which means the created_at
updated_at
will be null.
上面的代码工作正常。但这不能触及时间戳。这意味着created_at
updated_at
将为空。
However, if I changed the method insert
to create
:
但是,如果我将方法更改insert
为create
:
GroupRelation::create([
...
]);
It received the error:
它收到错误:
ErrorException in Model.php line 2983:
preg_match() expects parameter 2 to be string, array given
If I'm not wrong, it seems like create
can just accept 1 row at the same time, right?
如果我没记错的话,好像create
只能同时接受 1 行,对吗?
I know insert
is not a part of Eloquent
. Eloquent
fires timestamps but Query Builder
does not.
我知道insert
不是Eloquent
. Eloquent
触发时间戳但Query Builder
不触发。
So my questions are:
所以我的问题是:
insert
method can accept 3 rows at one time, but how can I fire the timestamp manually?- By
1.
I've tried to add'created_at' => date('Y-m-d H:i:s'),
at each items of the array. But the controller is unclear and horrible. - How can I pass an array of 3 items to
create
method in order to fire timestamps? - Is it a good idea to call
create
inside the loops?
insert
方法一次可以接受 3 行,但是如何手动触发时间戳?- 通过
1.
我尝试添加'created_at' => date('Y-m-d H:i:s'),
数组的每个项目。但是控制器不清楚且可怕。 - 如何将包含 3 个项目的数组传递给
create
方法以触发时间戳? create
在循环内部调用是个好主意吗?
PS. protected $guarded = []
was assigned to empty array so would not receive Mass Assignment Exception.
附注。protected $guarded = []
已分配给空数组,因此不会收到批量分配异常。
Thank you so much.
非常感谢。
回答by Alexey Mezenin
insert()
is a part of Query Builder, so it doesn't use Eloquent features.
insert()
是 Query Builder 的一部分,因此它不使用 Eloquent 功能。
You can't pass 3 items to create()
method, only one. And using create()
inside loops is terrible idea.
您不能将 3 个项目传递给create()
方法,只能传递一个。使用create()
内部循环是一个糟糕的主意。
You should add these fields manually when doing bulk insertion:
在进行批量插入时,您应该手动添加这些字段:
[
'application_id' => $applications[0]->a_id,
'group_id' => $group->id,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
回答by Rashedul Islam Sagor
You may use dateTime
class :
您可以使用dateTime
类:
[
'application_id' => $applications[0]->a_id,
'group_id' => $group->id,
'created_at' => new \dateTime,
'updated_at' => new \dateTime,
],