NoxxieNl
4 months ago

Save relation and polymorphic relation in one go

Posted 4 months ago by NoxxieNl

Hiya guys :-),

I have three tables:

Erpmails
- id
- name
Erpmail_attachments
- id
- erpmail_id
- attachable_id
- attachable_type
Erpmail_erpdocuments
- id
- name
- description

erpdocuments is an attachable type of attachments.

How can I insert a polymorphic relationship within attachments as the same time I am creating a relation to the erpmails table.

So far I only came up with:

$mail = Erpmail::find(1);

$attachment = new ErpmailAttachment;
$attachment->required = true;
$attachment->required_multiple_cases = false;
$attachment->use_letterhead = true;
$attachment->attachable_id = 1;
$attachment->attachable_type = (new ErpmailErpdocument)->getMorphClass();

$mail->attachments()->save($attachment);

i cant figure out how to attach the attachable model in one go, Any of you guys got a brilliant tip?

Thanks in advance :)

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