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
  • 1 Does your model use the use Illuminate\Database\Eloquent\Concerns\HasUuids; trait? I can partially recreate this via Model::first()->uuid returning the correct, full UUID "9c794818-994a-4e25-8c6a-2efcbe67f2c0" with that trait, but only returning "9" without that trait. – Tim Lewis Commented Nov 22, 2024 at 13:59
  • @TimLewis Thanks for your help. Found the problem, it is necessary to add a property to the model that works with UUID protected $keyType = 'string'; by default there int – Александр Инженер Commented Nov 22, 2024 at 14:04
  • Interesting, I guess that works? The HasUuids 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:06
  • @TimLewis For some reason this trait doesn't work for me use Illuminate\Database\Eloquent\Concerns\HasUuids; – Александр Инженер Commented Nov 22, 2024 at 14:19
  • Hmm... I wonder why. Unfortunately I don't have a Laravel 11 project to verify this. If this approach works for you that's totally fine too, I was just curious what approach you were taking, since in my experience, using uuids requires the additional logic contained in that Trait

    本文标签: Laravel UUID instead of regular bigInteger in models trims uuidStack Overflow