laravel uuid 未在查询中显示
laravel uuid not showing in query
我有一个 postgres 数据库 table,它通过 webpatser/laravel-uuid 包和 'readable' 网络使用 uuid 作为主键ID 来自 vinkla/hashids.
当我查询数据库时,如果我 dd() 响应,我会看到完整的 UUID,但如果我只是 return ,我反而得到一个整数。
假设我忽略了一些明显的东西,所以:
迁移
$table->uuid('id');
$table->string('web_id');
$table->primary('id');
型号
public function store()
{
$data = [
'id' => Uuid::generate(4)->string,
'web_id' => Hashids::encode(mt_rand(1,1000000)),
我假设在将数据转换为 json 时发生了一些事情,但我不确定我应该从哪里开始解决这个问题...
我在 artisan tinker 中也看到了相同的行为,fwiw:
>>> $result = App\Model::firstOrFail()
=> App\Model {#675
id: "587bb487-881d-417e-8960-fbecaa3b270b",
web_id: "Mxqv4LYP",
created_at: "2016-01-25 15:52:25+00",
updated_at: "2016-01-25 15:52:25+00",
}
>>> $result->id
=> 587
Eloquent 假设主键(即 named id
by default) is an integer, and it casts it to int
by default in the getCasts
方法:
public function getCasts()
{
if ($this->incrementing) {
return array_merge([
$this->getKeyName() => 'int',
], $this->casts);
}
return $this->casts;
}
这可以通过将主键添加到模型中指定主键不自动递增来覆盖:
$incrementing = false;
或者将模型的 $casts
属性 中的 id
列转换为 string
,如下所示:
protected $casts = [
'id' => 'string'
]
如 Eloquent: Attribute Casting 文档中所述。
我有一个 postgres 数据库 table,它通过 webpatser/laravel-uuid 包和 'readable' 网络使用 uuid 作为主键ID 来自 vinkla/hashids.
当我查询数据库时,如果我 dd() 响应,我会看到完整的 UUID,但如果我只是 return ,我反而得到一个整数。
假设我忽略了一些明显的东西,所以:
迁移
$table->uuid('id');
$table->string('web_id');
$table->primary('id');
型号
public function store()
{
$data = [
'id' => Uuid::generate(4)->string,
'web_id' => Hashids::encode(mt_rand(1,1000000)),
我假设在将数据转换为 json 时发生了一些事情,但我不确定我应该从哪里开始解决这个问题...
我在 artisan tinker 中也看到了相同的行为,fwiw:
>>> $result = App\Model::firstOrFail()
=> App\Model {#675
id: "587bb487-881d-417e-8960-fbecaa3b270b",
web_id: "Mxqv4LYP",
created_at: "2016-01-25 15:52:25+00",
updated_at: "2016-01-25 15:52:25+00",
}
>>> $result->id
=> 587
Eloquent 假设主键(即 named id
by default) is an integer, and it casts it to int
by default in the getCasts
方法:
public function getCasts()
{
if ($this->incrementing) {
return array_merge([
$this->getKeyName() => 'int',
], $this->casts);
}
return $this->casts;
}
这可以通过将主键添加到模型中指定主键不自动递增来覆盖:
$incrementing = false;
或者将模型的 $casts
属性 中的 id
列转换为 string
,如下所示:
protected $casts = [
'id' => 'string'
]
如 Eloquent: Attribute Casting 文档中所述。