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

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.

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

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思