preg_match() expects parameter 2 to be string, arr

2020-04-21 02:09发布

问题:

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 ?

回答1:

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.