没有不值得去解决的问题,也没有不值得去学习的技术!

Implementing UUID Fields in MySQL Tables With Laravel 6

The final generated SQL is as follows, which meets expectations

1. The database migration is implemented as follows, with an index added to the UUID type field.

PHP

    public function up()
    {
        $tableName = 'theme_installation';

        Schema::create($tableName, function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->uuid('theme_id')->comment('主题ID')->unique();
            $table->timestamps();
        });

    }

2. After running the migration, the table field type is char(36). See Figure 1.

After running the migration, the table field type is char(36)
Figure 1
SQL

CREATE TABLE `theme_installation` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `theme_id` char(36) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '主题ID',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `theme_installation_theme_id_unique` (`theme_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

3. In the model file’s constructor, set the UUID type field value to Str::orderedUuid() to leverage sorting functionality. Reference: https://itnext.io/laravel-the-mysterious-ordered-uuid-29e7500b4f8

PHP

use Illuminate\Support\Str;

    public function __construct(array $attributes = [])
    {
        $attributes['theme_id'] = Str::orderedUuid();
        parent::__construct($attributes);
    }

4. The implementation when actually performing a model insert.

PHP

$themeInstallation = new ThemeInstallation();
$themeInstallation->wp_theme_id = $wpTheme->id;
$themeInstallation->theme_custom_name = $custom_name;
$themeInstallation->save();

5. An error is reported during execution: Add [theme_id] to fillable property to allow mass assignment on [Modules\\ThemeStoreDB\\Entities\\ThemeInstallation]. See Figure 2.

Error reported during execution: Add [theme_id] to fillable property to allow mass assignment on [Modules\\ThemeStoreDB\\Entities\\ThemeInstallation].
Figure 2
JSON

{
    "message": "Add [theme_id] to fillable property to allow mass assignment on [Modules\\ThemeStoreDB\\Entities\\ThemeInstallation].",
    "status_code": 500,
    "debug": {
        "line": 332,
        "file": "E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php",
        "class": "Illuminate\\Database\\Eloquent\\MassAssignmentException",
        "trace": 
    }
}

6. Set the model’s theme_id property to be mass assignable.

PHP

protected $fillable = ['theme_id'];

7. The final generated SQL is as follows, which meets expectations. See Figure 3.

The final generated SQL is as follows, which meets expectations
Figure 3
SQL

insert into
  `table` (
    `theme_custom_name`,
    `role`,
    `processing`,
    `processing_failed`,
    `theme_id`,
    `theme_store_theme_id`,
    `wp_theme_id`,
    `updated_at`,
    `created_at`
  )
values
  (
    '',
    'unpublished',
    1,
    0,
    '966165f6-139a-464d-b2aa-77af8bc32a4a',
    9,
    88,
    '2022-05-25 11:51:06',
    '2022-05-25 11:51:06'
  )

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系