Alaa Mohammad
Alaa Mohammad

Reputation: 665

Laravel : Failure to save data

I want to create reset button that button will delete all the previous data and create new data. for the new data, each question must relate to all existing sectors.but when I try to save data I got this error

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

here is my code:

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);
            }

        }

Upvotes: 1

Views: 65

Answers (2)

Sulthan Allaudeen
Sulthan Allaudeen

Reputation: 11320

Don't insert the entire elements that you got inside foreach,

Just do this Inside your foreach

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

Note :

You shall add more elements inside the create() or you shall create a new array and then do create($yourNewArray)

Upvotes: 1

PRANAV
PRANAV

Reputation: 629

replace

CustomizeQuestion::create($question);

with

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

Upvotes: 0

Related Questions