laravel preg_match() 期望参数 2 是字符串,数组给定错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39158318/
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
preg_match() expects parameter 2 to be string, array given Error
提问by Developer
I'm trying to insert array but I'm getting error:-
我正在尝试插入数组,但出现错误:-
preg_match() expects parameter 2 to be string, array given
preg_match() 期望参数 2 是字符串,给定数组
My form below like :
我的表格如下:
{!! 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']) !!}
My controller store function :
我的控制器存储功能:
$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);
when i check dd($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
]
]
here what im doing wrong ?
这里我做错了什么?
回答by patricus
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.
看起来您正试图port_logs
在一个语句中插入多个。但是,该create()
方法仅用于创建模型的一个实例。您要么需要使用该insert()
语句,要么更新您的代码以foreach
通过您的$data
并发出多个create()
语句。
PortLog::insert($data);
// or
foreach($data as $row) {
PortLog::create($row);
}
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.
如果你只是想插入数据,而不想PortLog
实例化一堆实例,那么insert()
方法就是要走的路。如果您需要PortLog
为每一行实例化一个新实例,那么create()
方法就是要走的路。