Laravel:未能保存数据

时间:2015-12-10 11:27:03

标签: php laravel-5.1

我想创建重置按钮,该按钮将删除所有以前的数据并创建新数据。 对于新数据,每个问题必须与所有现有扇区相关。但是当我尝试保存数据时,我收到了此错误

ErrorException in Model.php line 542:
Argument 1 passed to Illuminate\Database\Eloquent\Model::create() must be of the type array, object given, called in C:\wamp\www\laravel\scoring-system\app\Http\Controllers\DashboardController.php on line 103 and defined

这是我的代码:

public function resetQuestions()
    {
        DB::table('customize_questions')->delete();
        $questions = Question::all();
        $sectors = Sector::all();
        foreach ($sectors as $sector ) {
            foreach ($questions as $question ) {

                $question['sector_id'] = $sector->id;
                CustomizeQuestion::create($question);
            }

        }

2 个答案:

答案 0 :(得分:1)

不要插入你在foreach中的所有元素,

在你的foreach中做这个

foreach ($questions as $question ) {
  CustomizeQuestion::create(['sector_id' => $sector->id]);
}

注意:

您应在create()内添加更多元素,或者您应创建一个新数组然后执行create($yourNewArray)

答案 1 :(得分:0)

替换

CustomizeQuestion::create($question);

CustomizeQuestion::create(array()($question));