ErrorException preg_replace():参数不匹配,pattern是一个字符串,而replacement是一个数组。 Laravel

时间:2014-05-21 15:48:52

标签: laravel

我有一个表单输入字段,它接受一组值:

<input name="subjects_studied[]" type="text" placeholder="" size="30" class="form-styles" >

在我的routes.php脚本中,我有一个像这样的闭包:

Route::post('apply', function() {

    $v = ApplicationForm::validate(Input::all());

    if ( $v->passes() ) {

        $data = array(

                 //  'subjects_studied' =>  Input::get('subjects_studied'), 
                   'subjects_studied' => (is_array(Input::get('subjects_studied'))) ? 
                                            implode(" , " , Input::get('subjects_studied')) : 
                                            Input::get('subjects_studied'),

                   );

          ApplicationForm::create($data);
    }
}

我尝试了两种配置,但他们都给了我一个错误:

ErrorException preg_replace(): Parameter mismatch, pattern is a string while replacement is an array.

作为掷骰子的最后一击,我试过了:

$subjects_studied = Input::get('subjects_studied');
            foreach($subjects_studied as &$studied){
                $_subjects[] = $studied;
            }

            $x = implode($_subjects);

$data = array('subjects_studied' => $x,)

你能帮我用laravel从HTML表单插入一个值数组吗?

1 个答案:

答案 0 :(得分:1)

我的理解是你需要一个循环来获取所有这些值并保存到数据库。

我要做的是:

$subjects_studied = Input::get('subjects_studied');
for ($i=0; $i < count($subjects_studied) ; $i++) { 
    $subject = new ApplicationForm;
    $subject->subjects_studied = $subjects_studied[$i]
    $subject->save(); 
}

或尝试使用foreach

foreach ($subjects_studied as $key => $value) {
    $subject = new ApplicationForm;
    $subject->subjects_studied = $value
    $subject->save();
}
祝你好运