【发布时间】:2016-01-25 16:44:42
【问题描述】:
我有一个 postgres 数据库表,它使用 uuid 作为其主键,通过 webpatser/laravel-uuid 包,以及通过 vinkla/hashids 的“可读”网络 ID >.
当我查询数据库时,如果我 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 时会发生一些事情,但我不确定从哪里开始解决这个问题...
我在工匠修补匠中也看到了相同的行为,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
【问题讨论】:
-
你的
id字段在数据库中的类型是什么,长度是多少? -
我正在使用 postgres,Postico 只是将其报告为 'uuid' 类型,如果这有帮助的话。
-
可能会尝试将结果转换为数组
->toArray()并制作dd(); -
谢谢 - 所以,在 tinker 中,
$result和dd($result)给'id' => (uuid...),而$result->toArray()和dd($result->toArray())都给'id' => (integer...)
标签: php postgresql laravel eloquent uuid