[英]spatie/laravel-medialibrary change primary key
I'm using the package spatie/laravel-medialibrary and I want to change the primaryKey on their modal called Media, without editing the package src file.我正在使用 package spatie/laravel-medialibrary并且我想更改其名为 Media 的模态上的主键,而不编辑 package src 文件。
In my project, I'm using uuids as primary keys for all my models, so naturally, I want to do the same thing for the Media.php model offered by this package.在我的项目中,我使用 uuids 作为我所有模型的主键,所以很自然,我想为这个 ZEFE90A8E604A7C6BFD762321E409CEE4AC0B6E841963CZ model 提供的 ZEFE90A8E604A7C840E88D03A867 做同样的事情。
I already changed the migration to reflect that, by removing the line $table->bigInteger('id')
and changing the line $table->uuid('uuid')->nullable();
我已经通过删除行$table->bigInteger('id')
并更改行$table->uuid('uuid')->nullable();
来更改迁移以反映这一点to table->uuid('uuid')->unique()->primary();
to table->uuid('uuid')->unique()->primary();
However, now I also want to let the model know I'm using a different key, by setting up protected $primaryKey = 'uuid';
但是,现在我还想让 model 知道我正在使用不同的密钥,方法是设置protected $primaryKey = 'uuid';
and protected $keyType = 'string';
和protected $keyType = 'string';
but I can't find a way to do this outside the packages src file for the Media.php model但我找不到在 Media.php model 的包 src 文件之外执行此操作的方法
Basically, what I want to end up doing is just implementing the HasMedia interface and using the InteractsWithMedia trait on my Profile model, like this:基本上,我最终要做的只是实现 HasMedia 接口并在我的 Profile model 上使用 InteractsWithMedia 特征,如下所示:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Profile extends Model implements HasMedia
{
use InteractsWithMedia;
}
Any suggestions on how to achieve this?关于如何实现这一目标的任何建议?
Thanks.谢谢。
Spatie's medialibrary package gives you the option to use your own media model, as described in their docs . Spatie 的媒体库 package 让您可以选择使用自己的媒体 model,如他们的文档中所述。
Just create your custom model and extend the library's Media
model.只需创建您的自定义 model 并扩展库的Media
model。 You can then modify that csutom model to fit your needs.然后,您可以修改该 csutom model 以满足您的需求。
use Spatie\MediaLibrary\MediaCollections\Models\Media as BaseMedia;
class Media extends BaseMedia
{
protected $primaryKey = 'uuid';
protected $keyType = 'string';
public $incrementing = false;
// ...
}
Remember to set the media_model
key in config/media-library.php
to your model's FQCN.请记住将config/media-library.php
中的media_model
键设置为模型的 FQCN。
'media_model' => App\YourMediaModel::class,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.