无法在模型中使用id吸气剂(我使用uuid类型) [英] Not work id getter in model (i use uuid type)

查看:91
本文介绍了无法在模型中使用id吸气剂(我使用uuid类型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模型中将主键设置为uuid.

I make the primary key as uuid in Model.

使用uuid主键进行迁移:

Migration with uuid primary key:

Schema::create('brands', function (Blueprint $table) {
    $table->uuid('id')->unique()->primary();
    $table->string('title');
    $table->timestampsTz();
});

检查模型:

Psy Shell

>>> $x = App\Models\Brand::find('e025b8aa-c71e-42ca-b87c-7ee27695b83a');
=> App\Models\Brand {#720
  id: "e025b8aa-c71e-42ca-b87c-7ee27695b83a",
  title: "Batman",
  created_at: "2017-04-28 23:51:41+10",
  updated_at: "2017-04-28 23:51:41+10",
}

访问模型时,它包含正确的数据.

When accessing the model, it contains the correct data.

>>> $x->title
=> "Batman"
>>> $x->id
=> 0

为什么为零?

推荐答案

Eloquent具有一些默认约定,因此对您的模型,表,约束等有一些期望. 幸运的是,您可以更改它们:

Eloquent has some default conventions and thus has some expectations on your model, table, constraints etc... Luckily, you can change them:

从Laravel文档中获得:

主键

口才也将假设每个表都有一个主键列 命名的ID.您可以定义$ primaryKey属性以覆盖此属性 惯例.

Eloquent will also assume that each table has a primary key column named id. You may define a $primaryKey property to override this convention.

此外,Eloquent假定主键是增量键 整数值,这意味着默认情况下,主键为 自动转换为int.如果您希望使用非增量或 非数字主键,您必须设置公共$ incrementing 模型上的属性设置为false.

In addition, Eloquent assumes that the primary key is an incrementing integer value, which means that by default the primary key will be cast to an int automatically. If you wish to use a non-incrementing or a non-numeric primary key you must set the public $incrementing property on your model to false.

关于您的问题:为什么要零?"

Regarding your question: "Why zero?"

因为Eloquent将"e025b8aa-c71e-42ca-b87c-7ee27695b83a"的字符串显式转换为int值,结果为int 0 ...

Because Eloquent casts the string of "e025b8aa-c71e-42ca-b87c-7ee27695b83a" explicitly to an int value and that result is int 0...

这篇关于无法在模型中使用id吸气剂(我使用uuid类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆