admin管理员组文章数量:1122832
I'm trying to work with uuid instead of regular integer id.
Migration
Schema::create('products', function (Blueprint $table) {
$table->uuid('id')->primary()->unique()->index();
$table->string('name',100);
$table->text('description');
});
I create a record in the database, read it and try to access the id property
dd (Product::first());
"id" => "3246c4a7-9757-11e8-80d0-00155dbf5a2a"
"name" => "tea"
"description" => "dummy description"
The results are correct, but if I try to access the property Product::first()->id
directly, I get a truncated UUD.
3246 // app/Http/Controllers/Web/ProductsController.php:21
The same situation if I convert to an array or a json Product::first()->toArray()
array:3 [▼ // app/Http/Controllers/Web/ProductsController.php:21
"id" => 3246
"name" => "tea"
"description" => "dummy description"
Why does Laravel 11.33.2 truncate the uuid when accessing through object properties?
I'm trying to work with uuid instead of regular integer id.
Migration
Schema::create('products', function (Blueprint $table) {
$table->uuid('id')->primary()->unique()->index();
$table->string('name',100);
$table->text('description');
});
I create a record in the database, read it and try to access the id property
dd (Product::first());
"id" => "3246c4a7-9757-11e8-80d0-00155dbf5a2a"
"name" => "tea"
"description" => "dummy description"
The results are correct, but if I try to access the property Product::first()->id
directly, I get a truncated UUD.
3246 // app/Http/Controllers/Web/ProductsController.php:21
The same situation if I convert to an array or a json Product::first()->toArray()
array:3 [▼ // app/Http/Controllers/Web/ProductsController.php:21
"id" => 3246
"name" => "tea"
"description" => "dummy description"
Why does Laravel 11.33.2 truncate the uuid when accessing through object properties?
Share Improve this question edited Nov 22, 2024 at 13:55 Александр Инженер asked Nov 22, 2024 at 13:50 Александр ИнженерАлександр Инженер 1,1902 gold badges25 silver badges42 bronze badges 5
use Illuminate\Database\Eloquent\Concerns\HasUuids;
trait? I can partially recreate this viaModel::first()->uuid
returning the correct, fullUUID
"9c794818-994a-4e25-8c6a-2efcbe67f2c0"
with that trait, but only returning"9"
without that trait. – Tim Lewis Commented Nov 22, 2024 at 13:59protected $keyType = 'string';
by default thereint
– Александр Инженер Commented Nov 22, 2024 at 14:04HasUuids
trait does something similar:if (in_array($this->getKeyName(), $this->uniqueIds())) { return 'string'; }
. Any reason you're not using this Trait? – Tim Lewis Commented Nov 22, 2024 at 14:06use Illuminate\Database\Eloquent\Concerns\HasUuids;
– Александр Инженер Commented Nov 22, 2024 at 14:19uuid
s requires the additional logic contained in that Trait本文标签: Laravel UUID instead of regular bigInteger in models trims uuidStack Overflow