Based on an empty Laravel 5.8, this repository shows how the Spatie Media Library could handle a polymorphic many to many DB.
git clone
composer install
npm install
php artisan key:generate
Follow the install instructions to init Laravel, copy the .env.example file to a .env file and set your database information to get started. Then generate the data to it via
php artisan migrate --seed
A Mediable table is added to have a many-to-many polymorphic relationship possible to any model that has a media :
Schema::create('mediables', function (Blueprint $table) {
$table->integer("media_id");
$table->integer("mediable_id");
$table->string("mediable_type");
$table->string("tag");
$table->integer("order");
$table->timestamps();
});
To add the possibility for the models to have this polymorphic link, we create a trait :
<?php
namespace App\Traits;
trait HasMediaTuto
{
/**
* @return mixed
*/
public function medias()
{
return $this->morphToMany(\App\Media::class, 'mediable');
}
}
And in any model plus your user model : use HasMediaTuto
👤 Epistol
Give a ⭐️ if this project helped you!
This README was generated with ❤️ by readme-md-generator