jackeratarina/laravel-uuid-database-queue

这使Laravel可以使用UUID作为作业主键,并实现适当的交互。

dev-main 2024-08-06 03:15 UTC

This package is auto-updated.

Last update: 2024-09-06 03:31:36 UTC


README

使用UUID作为主键的Laravel数据库队列

问题:在使用MySQL时,如果id的数据类型为CHAR(36)并且插入记录时没有设置值,则添加作业时会抛出错误。

此包提供服务提供者和必要的类,允许您使用UUID作为主键插入记录。UUID是有序的,并使用Laravel的Str::orderedUuid()

安装包

要安装此包,请使用以下命令

composer require jackeratarina/laravel-uuid-database-queue

Fresh Install of Laravel

如果您有一个全新的Laravel项目,您需要使用以下Laravel命令创建jobs表的迁移。

php artisan queue:table

接下来,打开名为create_jobs_table的文件,并做出以下更改

Schema::create('jobs', function (Blueprint  $table) {
	// $table->bigIncrements('id'); // remove this line
	$table->uuid('id')->primary(); // add this line
	$table->string('displayName'); // add this line
	$table->string('queue')->index();
	$table->longText('payload');
	$table->unsignedTinyInteger('attempts');
	$table->unsignedInteger('reserved_at')->nullable();
	$table->unsignedInteger('available_at');
	$table->unsignedInteger('created_at');
});

运行迁移部分继续

已迁移作业表的现有项目

在添加服务提供者之前,请使用php artisan migrate:status检查是否可以看到迁移2021_10_24_161222_change_id_column_in_jobs_table。如果您看到了它,请跳转到运行迁移

如果您已经运行了迁移并且表已经存在于您的数据库中,您需要首先注册包的服务提供者。打开您的项目中的config/app.php文件,并将以下行添加到providers数组中。

jackeratarina\LaravelUuidDatabaseQueue\LaravelUuidDatabaseQueueProvider::class

之后,运行迁移。

运行迁移

现在所有迁移都已设置好,请使用以下命令

php artisan migrate

设置服务提供者

最后一步是将Illuminate\Queue\QueueServiceProvider::class替换为jackeratarina\LaravelUuidDatabaseQueue\QueueServiceProvider::class,打开config/app.php文件。删除或注释掉providers数组中的Illuminate\Queue\QueueServiceProvider::class行,并添加jackeratarina\LaravelUuidDatabaseQueue\QueueServiceProvider::class

您已经设置完毕...