douyi9787 2016-08-26 04:29
浏览 136
已采纳

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 ?

  • 写回答

1条回答 默认 最新

  • dongyunwei8596 2016-08-26 05:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?