简体   繁体   English

spatie/laravel-medialibrary 更改主键

[英]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.

相关问题 spatie/laravel-medialibrary - 能够“即时”添加每个媒体 model 的转换? - spatie/laravel-medialibrary - Ability to add conversion per media model "on the fly"? Laravel-medialibrary:如何设置文件和转换的所有者? - Laravel-medialibrary: How to set owner of files and conversions? 未在 Amazon S3 上使用 laravel-medialibrary 创建 Laravel 媒体转换 - Laravel Media Conversions not being created with laravel-medialibrary on Amazon S3 我在使用 Spatie\MediaLibrary\IntercatsWithMedia 时出错; 并使用 Spatie\MediaLibrary\HasMedia; 在 laravel 安装 spatie 媒体库后 - I have an Error in use Spatie\MediaLibrary\IntercatsWithMedia; and use Spatie\MediaLibrary\HasMedia; in laravel after installing spatie media library 使用 Spatie\\MediaLibrary 上传文件时 Laravel 包错误 - Laravel package error on file uploading using Spatie\MediaLibrary 如何使用 Laravel Spatie medialibrary v9 保存上传的图像? - How can I save uploaded image using Laravel Spatie medialibrary v9? 如何在 laravel 迁移中更改主键 - How to change primary key in laravel migration Laravel更改users表的默认主键 - Laravel change default primary key of users table 使用 SQLite 更改 Laravel 迁移中的主键 - Change primary key in Laravel migration with SQLite Spatie Medialibrary 自定义自定义目录作为前缀 - Spatie Medialibrary Custom Custom Directory As Prefix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM