Laravel Model relationship "Page have many Attachments"

  eloquent--relationship, laravel, php

I have models:

Page:

  • id
  • slug

Image

  • id
  • file

Video

  • id
  • file

I need the Page model to have a relation with several Image and Video models through one relationship, like

foreach($page->attachments as $attachment)
{
    // $attachment can be Image or Video
}

And inserts like

$attachments = [$image, $video];
$page->attachments()->saveMany($attachments);

I tried to make a morph relationship, but nothing comes of it, please help.

Source: Laravel

Leave a Reply