Attach or sync in one to many relation laravel

Question Model:

$this->hasMany('App\Option');

Option Model:

$this->belongsTo('App\Question');

Question Controller

$question = new Question;
$question->question = 'Question';
$question->options()->attach(['option1', 'option2', 'option3', 'option4']);

I'm trying to attach the options data, but it says error "Call to undefined method Illuminate\Database\Query\Builder::attach()"

How to attach and sync that in the right way?

1 answer

  • answered 2018-02-13 02:23 NONAME

    Relationship one to many does not have method attach().attach() method working only for relationship ManytoMany. You can just use id one of your model.