0

我正在使用 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”)值([], {"title":"Magnam nulla perfere","code":"Temporibus in ut cul","created_by":"2d03731c-9c71-4238-bdfc-03e3f556ddef","end_date":"2012-09-29", “开始日期”:“2016 年 12 月 2 日”,“货币”:“PKR”,“状态”:“进行中”,budget":"32","objectives":"Architecto adipisci","specificobjectives":"Eveniet harum repre","abreviation":"Aut iusto quod quia","pcr_deadline":"1976-04-28","捐助者":"联合国儿童基金会","donor_fp_name":"Tanner Noble","donor_fp_email":"gyvimyz@mailinator.com","donor_fp_designation":"Ut vel quidem sed fa","donor_fp_mobile":"Qui sat provident", "donor_fp_phone_office":"+1 (832) 163-8003","brsp_fp_email":"wygihihyw@mailinator.com","brsp_fp_name":"Myles 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, App\Models\Project, 2d03731c-9c71-4238-bdfc-03e3f556ddef, App\ Models\User, http://localhost:8000/project/store, 127.0.0.1, Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like 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))2021-07-13 15:32:09"},创建,974afba9-556e-4ecd-bd42-aaf12ce9aab0,App\Models\Project,2d03731c-9c71-4238-bdfc-03e3f556ddef,App\Models\User,http:// /localhost:8000/project/store, 127.0.0.1, Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like 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))2021-07-13 15:32:09"},创建,974afba9-556e-4ecd-bd42-aaf12ce9aab0,App\Models\Project,2d03731c-9c71-4238-bdfc-03e3f556ddef,App\Models\User,http:// /localhost:8000/project/store, 127.0.0.1, Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like 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))

4

1 回答 1

1

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

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变形。

于 2021-07-13T17:27:43.987 回答