I searched but didn't found any specific issue may be many are using normal incrementing integer. The problem is particularly due to uuid field in tables and media library tables uses big int to store other's model foreign key.
Problem is user uses unsigned integer as primary key but Post uses uuid as primary key? Is there anyway to solve this issue .
Thanks for this awesome package :)
Hi! I published and rewriting database migration. Migration lookls like this;
Schema::create('media', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->uuid('model_id');
$table->string('model_type');
$table->index(['model_id', 'model_type']);
$table->string('collection_name');
$table->string('name');
$table->string('file_name');
$table->string('mime_type')->nullable();
$table->string('disk');
$table->unsignedInteger('size');
$table->jsonb('manipulations');
$table->jsonb('custom_properties');
$table->unsignedInteger('order_column')->nullable();
$table->nullableTimestamps();
});
I using medialibrary 6.0 in this project
@kalinichenko88 Ok i may use it but I guess it will create problem with non uuid models right?
@bloggervista yes, only one solving may used
@bloggervista not good using different primary keys in one project, IMHO
@kalinichenko88 Yeah I agree but I usually use other package and I cannot change package code as it will be updated right.. Although my project is fully UUID. Is there any solutions for such bro ?
Thanks
@bloggervista I rewriting primary keys and foreign keys in all migrations on side packages. Problems may be, but it's good solution for me.
If problem will be in business logic Laravel DI or package configuration should solves this :-)
@kalinichenko88 yeah but that is abit tedious . Cannot we make this package flexible enough to support different key. May be lets say model_id represents other unique key like slug etc.?
It is tedious because I will need to change package model incrementing etc to false and override various methods like boot etc to generate uuid for that package and even need to modify every migrations etc..
Thanks
@bloggervista I think it's impossible without hacks
Can we use model slug or other unique key instead of primary key?
I always like to hide primary key instead of exposing to public?
You can hide the ids by using a path generator
https://docs.spatie.be/laravel-medialibrary/v7/advanced-usage/using-a-custom-directory-structure
Hi, My question's different. How can I set default value to this $table->uuid('model_id'); ?
You can use your own model (as explained in https://docs.spatie.be/laravel-medialibrary/v7/advanced-usage/using-your-own-model).
Example:
<?php
namespace App\Models;
use Illuminate\Support\Str;
use Spatie\MediaLibrary\Models\Media as BaseMedia;
class Media extends BaseMedia
{
public $incrementing = false;
protected static function boot()
{
static::creating(function (Model $model) {
if (is_null($model->getOriginal('id'))) {
$model->id = Str::uuid()->toString();
}
});
}
}
@harrysbaraini's answer is so close, but it is missing parent::boot() which will cause Undefined index: App\Models\Media
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Spatie\MediaLibrary\Models\Media as BaseMedia;
class Media extends BaseMedia
{
public $incrementing = false;
/**
* @see https://github.com/spatie/laravel-medialibrary/issues/1112#issuecomment-531477078
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::creating(function (Model $model) {
if (is_null($model->getOriginal('id'))) {
$model->id = Str::uuid()->toString();
}
});
}
}
Most helpful comment
Hi! I published and rewriting database migration. Migration lookls like this;
I using medialibrary 6.0 in this project