7 months ago

When and how to use associate() method?

Posted 7 months ago by DevMaster


So Eloquent's attach() method is for many-to-many situations; and I am using that without issue.

The equivalent for 1-to-many relationships appears to be associate(), but I can not get this to work. (failing with either "undefined method" or it doesn't auto add the foreign key.) I am able to simply use $singleModel->manyRelatedModels->save(new RelatedModel()) instead.

But I'm curious and wondering if anybody can shed any light on the associate() method?

Best regards, Dev Master

Please sign in or create an account to participate in this conversation.