preg_match()期望参数2为字符串,给定错误的数组

问题描述:

I'm trying to insert array but I'm getting error:-

preg_match() expects parameter 2 to be string, array given

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)

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 ?

我正在尝试插入数组,但我收到错误: - p>

preg_match()期望参数2为字符串,给定数组 p> blockquote>

我的表单如下: p> {!! 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'])!!} pre>

我的控制器存储功能: p>

  $ 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); 
  code>   pre> 
 
 

当我检查 dd($ data) strong> p>

 数组时:2 [▼
 0 =>  ; 数组:3 [▼
“description”=>  “des”
“log_time”=>  “”
“call_id”=>  16 
] 
 1 => 数组:3 [▼
“description”=>  “”
“log_time”=>  “hi”
“call_id”=>  16 
] 
] 
  code>  pre> 
 
 

这里我做错了什么? p> div>

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);
}

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.