From 0b1d3e93f08bb21d0d477228824b82c8ed16e3a9 Mon Sep 17 00:00:00 2001 From: Waad Mawlood <44348636+waadmawlood@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:18:17 +0300 Subject: [PATCH] implement model from config media and enable uuid for migration related and id from uuid config file --- config/config.php | 10 +++++ .../create_media_uuid_table.php.stub | 40 +++++++++++++++++++ src/MediaServiceProvider.php | 8 ++-- src/Traits/HasManyMedia.php | 2 +- src/Traits/HasOneMedia.php | 2 +- 5 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 database/migrations/create_media_uuid_table.php.stub diff --git a/config/config.php b/config/config.php index 2e1c4fc..41ff694 100644 --- a/config/config.php +++ b/config/config.php @@ -14,6 +14,16 @@ | */ + /* + * The model you want to use as a Media model + */ + 'model' => Waad\Media\Media::class, + + /* + * Enable Uuid Type only migration related `Uuid`, `nullableUuidMorphs` and `uuidMorphs` + */ + 'uuid' => false, + /* * Default disk configration of path in file system in config/filesystem.php */ diff --git a/database/migrations/create_media_uuid_table.php.stub b/database/migrations/create_media_uuid_table.php.stub new file mode 100644 index 0000000..3ab5838 --- /dev/null +++ b/database/migrations/create_media_uuid_table.php.stub @@ -0,0 +1,40 @@ +uuid('id')->primary(); + $table->uuidMorphs('model'); + $table->string('base_name'); + $table->string('file_name')->nullable(); + $table->string('path')->nullable(); + $table->integer('index')->default(1); + $table->string('label')->nullable(); + $table->string('disk')->nullable(); + $table->string('directory')->nullable(); + $table->string('mime_type')->nullable(); + $table->unsignedBigInteger('file_size')->default(0); + $table->boolean('approved')->default(config('media.default_approved', true)); + $table->nullableUuidMorphs('user'); + $table->timestamps(); + $table->softDeletes(); + }); + } + + /** + * Reverse the migrations. + */ + public function down() + { + Schema::dropIfExists('media'); + } +} diff --git a/src/MediaServiceProvider.php b/src/MediaServiceProvider.php index 5147ece..a91e4da 100644 --- a/src/MediaServiceProvider.php +++ b/src/MediaServiceProvider.php @@ -29,15 +29,15 @@ public function register() } $this->publishes([ - __DIR__.'/../config/config.php' => config_path('media.php'), + __DIR__ . '/../config/config.php' => config_path('media.php'), ], 'config'); + $is_uuid = config('media.uuid', false); + $path = $is_uuid ? '/../database/migrations/create_media_uuid_table.php.stub' : '/../database/migrations/create_media_table.php.stub'; if (!class_exists('CreateMediaTable')) { $this->publishes([ - __DIR__.'/../database/migrations/create_media_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_media_table.php'), + __DIR__ . $path => database_path('migrations/' . date('Y_m_d_His', time()) . '_create_media_table.php'), ], 'migrations'); } - - } } diff --git a/src/Traits/HasManyMedia.php b/src/Traits/HasManyMedia.php index f881a5f..0ef124c 100644 --- a/src/Traits/HasManyMedia.php +++ b/src/Traits/HasManyMedia.php @@ -18,6 +18,6 @@ trait HasManyMedia */ public function media() { - return $this->morphMany(Media::class, 'model')->orderBy(config('media.order.column'), config('media.order.type')); + return $this->morphMany(config('media.model', Media::class), 'model')->orderBy(config('media.order.column'), config('media.order.type')); } } diff --git a/src/Traits/HasOneMedia.php b/src/Traits/HasOneMedia.php index e9a286a..d6f4b60 100644 --- a/src/Traits/HasOneMedia.php +++ b/src/Traits/HasOneMedia.php @@ -15,6 +15,6 @@ trait HasOneMedia */ public function media() { - return $this->morphOne(Media::class, 'model')->latest(); + return $this->morphOne(config('media.model', Media::class), 'model')->latest(); } }