Laravel 审计和 UUIDS?

问题描述

我正在使用 Laravel 审计,它在我认为的 UUID 上显示错误,我已经从 http://www.laravel-auditing.com/docs/9.0/installation 安装并配置了 Laravel 审计并将 user_id 和 id 更改为 UUID,其中认类型为 bigInteger

  public function up()
{
    Schema::create('audits',function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->string('user_type')->nullable();
        $table->uuid('user_id')->nullable();
        $table->string('event');
        $table->morphs('auditable');
        $table->text('old_values')->nullable();
        $table->text('new_values')->nullable();
        $table->text('url')->nullable();
        $table->ipAddress('ip_address')->nullable();
        $table->string('user_agent',1023)->nullable();
        $table->string('tags')->nullable();
        $table->timestamps();
        
    });
}

我还在audit.PHP中使用了UUIDs Trait,就像我在其他项目模型中所做的那样我迁移了它在存储数据时创建了一个表的项目,它显示以下错误

sqlSTATE[22P02]:无效的文本表示:7 错误:无效的输入 bigint 类型的语法:“974afba9-556e-4ecd-bd42-aaf12ce9aab0”(sql: 插入“审计”(“old_values”、“new_values”、“event”、 "auditable_id","auditable_type","user_id","user_type","url","ip_address","user_agent","tags","id","updated_at","created_at") values ([],{"title":"Magnam nulla perfere","code":"Temporibus in ut cul","created_by":"2d03731c-9c71-4238-bdfc-03e3f556ddef","end_date":"2012-09-29","start_date":"2016-12-02","currency":"PKR","status":"进行中","budget":"32","objectives":"Architecto adipisci","specificobjectives":"Eveniet harum repre","abreviation":"Aut iusto quod quia","pcr_deadline":"1976-04-28","donor":"UNICEF","donor_fp_name":"Tanner Noble","donor_fp_email":"gyvimyz@mailinator.com","donor_fp_designation":"Ut vel quidem sed fa","donor_fp_mobile":"坐下 公积金","donor_fp_phone_office":"+1 (832) 163-8003","brsp_fp_email":"wygihihyw@mailinator.com","brsp_fp_name":"迈尔斯 Cameron","brsp_fp_designation":"Laboris ut eius ut e","brsp_fp_mobile":"Doloribus quis possi","brsp_fp_phone_office":"+1 (891) 256-8645","re​​porting_frequency":"每月","id":"974afba9-556e-4ecd-bd42-aaf12ce9aab0","updated_at":"2021-07-13 15:32:09","created_at":"2021-07-13 15:32:09"},创建, 974afba9-556e-4ecd-bd42-aaf12ce9aab0,应用\模型\项目, 2d03731c-9c71-4238-bdfc-03e3f556ddef,应用\模型\用户, http://localhost:8000/project/store,127.0.0.1,Mozilla/5.0 (Windows 新台币 10.0; Win64; x64) AppleWebKit/537.36(KHTML,像 Gecko) Chrome/91.0.4472.124 Safari/537.36,?,eda36c0b-13c3-42c1-a345-fe2bb4b27520,2021-07-13 15:32:09,2021-07-13 15:32:09))

解决方法

documentation 声明在模型中使用 UUID 而非自动递增 ID 时,您必须在 audits 表迁移中进行以下更改。

自动递增 id 上的 UUID

一些开发人员更喜欢使用 UUID 而不是自动递增的 ID。如果是这种情况,请确保像这样更新 up() 方法:

对于User,更改为

$table->nullableMorphs('user');

$table->uuid('user_id')->nullable();
$table->string('user_type')->nullable();
$table->index([
    'user_id','user_type',]);

对于 Auditable 模型,从

更改
$table->morphs('auditable');

$table->uuid('auditable_id');
$table->string('auditable_type');
$table->index([
   'auditable_id','auditable_type',]);

您已经更改了 User 变形,但您忘记了 Auditable 变形。