# Many To Many (polymorphic)
php artisan make:migration create_photoables_table --create=photoables
public function up()
{
Schema::create('photoables', function (Blueprint $table) {
$table->bigInteger('photo_id');
$table->bigInteger('photoable_id');
$table->string('photoable_type');
});
}
// Photo Model
public function products() : MorphToMany
{
return $this->morphedByMany(Product::class, 'photoable');
}
public function manufacturers(): MorphToMany
{
return $this->morphedByMany(Manufacturer::class, 'photoable');
}
// Manufacturer Model
public function photos(): morphToMany
{
return $this->morphToMany(Photo::class, 'photoable');
}
// Product Model
public function photos(): morphToMany
{
return $this->morphToMany(Photo::class, 'photoable');
}
# Create & Attach
# Create
$manufacturer = Manufacturer::create([
'name' => 'Example Name 1'
]);
$manufacturer->photos()->create([
'name' => 'Example Name 1 Photo'
]);
# Attach
$product = Product::create([
'name' => 'Arcs'
]);
$photo = Photo::create([
'name' => 'Arcs'
]);
$product->photos()->attach($photo);