问题描述
我正在尝试插入数组,但出现错误:-
I'm trying to insert array but I'm getting error:-
我的下方表单如下:
{!! Form::text('description[]',null,['class' => 'input-field input-sm','v-model'=>'row.description']) !!}
{!! Form::text('log_time[]',null,['class' => 'input-field input-sm','v-model'=>'row.log_time']) !!}
我的控制器存储功能:
$this->validate($request, $this->rules);
$data = array();
foreach($request->description as $key=>$value){
$data[]=[
'description'=> $value,
'log_time'=> $request->log_time[$key],
'call_id'=>$call->id,
];
}
PortLog::create($data);
当我检查 dd($ data)
array:2 [▼
0 => array:3 [▼
"description" => "des"
"log_time" => ""
"call_id" => 16
]
1 => array:3 [▼
"description" => ""
"log_time" => "hi"
"call_id" => 16
]
]
这里我做错了什么?
推荐答案
似乎您正在尝试在一个语句中插入多个port_logs
.但是,create()
方法仅用于创建模型的一个实例.您要么需要使用insert()
语句,要么通过您的$data
将代码更新为foreach
并发出多个create()
语句.
It looks like you're attempting to insert multiple port_logs
in one statement. However, the create()
method is only meant to create one instance of a model. You either need to use the insert()
statement, or update your code to foreach
through your $data
and issue multiple create()
statements.
PortLog::insert($data);
// or
foreach($data as $row) {
PortLog::create($row);
}
如果您只想插入数据,而又不想实例化一堆PortLog
实例,那么insert()
方法是您的理想选择.如果您需要为每一行实例化一个新的PortLog
实例,那么create()
方法就是您的理想选择.
If you just want to insert the data, and you don't want to instante a bunch of PortLog
instances, then the insert()
method is the way to go. If you need to instantiate a new PortLog
instance for each row, then the create()
method is the way to go.
这篇关于preg_match()期望参数2为字符串,给定错误的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!