Skip to content

Commit

Permalink
implement model from config media and enable uuid for migration relat…
Browse files Browse the repository at this point in the history
…ed and id from uuid config file
  • Loading branch information
waadmawlood authored Oct 21, 2023
1 parent 107e7bd commit 0b1d3e9
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 6 deletions.
10 changes: 10 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
40 changes: 40 additions & 0 deletions database/migrations/create_media_uuid_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMediaTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::create('media', function (Blueprint $table) {
$table->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');
}
}
8 changes: 4 additions & 4 deletions src/MediaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}


}
}
2 changes: 1 addition & 1 deletion src/Traits/HasManyMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
2 changes: 1 addition & 1 deletion src/Traits/HasOneMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 0b1d3e9

Please sign in to comment.