dimanche 1 novembre 2015

Laravel eloquent model save error

I am getting an error when trying to save my eloquent model as follows: Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of Illuminate\Database\Eloquent\Model, string given,

I have a model Question it hasMany AnswerOption on the other side AnswerOption belongsTo Question

I have a model QuestionRevision it hasMany QuestionRevisionAnswerOption on the other side QuestionRevisionAnswerOption belongsto QuestionRevision

Basically I want to copy the AnswerOptions from a Question I have retrieved to a new instance of QuestionRevision (as QuestionRevisionAnswerOptions).

I have saved a new QuestionRevision ($origRev) successfully. Now I am trying to add the QuestionRevisionAnswerOptions that go with it. This is where I get the error Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of Illuminate\Database\Eloquent\Model, string given,

I have tried saving the QuestionRevisionAnswerOptions all at once as an array using saveMany and also individually but I get the same error either way.

Here is the relevant code:

$original = Question::findOrFail($qId);  // find the original question
$origRev = new QuestionRevision();
// copy some stuff from original to origRev
$origRev->save();  // works to here

// now get the answerOptions from original and copy to $origRev
foreach ($original->answerOptions as $ao)
{
    $answerOption = new QuestionRevisionAnswerOption(['answer_text'=> $ao->answer_text,
                                                 'answer_explanation'   => $ao->answer_explanation,
                                                  'answer_option_id'     => $ao->id,
                                                  'is_correct'           => $ao->is_correct
                                                 ]);


    $origRev->revisionAnswerOptions()->save($answerOption); //This is the line that generates the error.
}

Why am I getting this error and how can I correct it?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1KQmuXK
via IFTTT

Aucun commentaire:

Enregistrer un commentaire