【问题标题】:Laravel Audit and UUIDS?Laravel 审计和 UUIDS?
【发布时间】:2021-07-13 16:00:48
【问题描述】:

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

  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: 插入“审计”(“旧值”、“新值”、“事件”、 “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" “状态”:“进行中”,“预算”:“32”,“目标”:“建筑师” adipisci","具体目标":"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":"迈尔斯 卡梅伦","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))

【问题讨论】:

    标签: php laravel logging eloquent laravel-auditing


    【解决方案1】:

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

    UUID 超过自动递增 id

    一些开发者更喜欢使用 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 变形。

    【讨论】:

    • 另外,如果您已经运行了默认迁移,则必须回滚并重新应用新迁移(或进行更改迁移)。
    猜你喜欢
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 2019-04-15
    • 2020-07-18
    • 1970-01-01
    • 2021-08-18
    • 2012-04-28
    相关资源
    最近更新 更多