pluck('id')将字符串值转换为零

时间:2017-11-05 13:13:44

标签: laravel laravel-5 eloquent laravel-5.5

我有一个主键为string类型的表,用于存储uuid值。当我查询数据时一切都很好。但是当我使用pluck('id')来获取所有id的数组时,我得到了一些零! 我尝试用pluck('firstName')测试它并返回名字数组。当我用'firstName'替换'id'时,我得到零值数组。

拔出会自动将id值转换为int吗?有解决方法吗?

我使用Laravel 5.5

$p = \App\Profile::all()->take(5)
=> Illuminate\Database\Eloquent\Collection {#1015
     all: [
       App\Profile {#980
         id: "client.121138f1-e999-35a1-a16",
         phone: "496.533.3798",
         firstName: "Gabriella",
         lastName: "Steuber",
         company: "",
         email: "tyra.raynor@example.com",
         city_id: 1,
         address: """
           353 Nolan Stravenue\n
           Gudrunshire, MN 36601-6307
           """,
         isCompany: 0,
         status: 1,
         state: "active",
         role: "client",
         created_at: "2017-11-05 11:59:49",
         updated_at: "2017-11-05 11:59:49",
         deleted_at: null,
       },
       App\Profile {#1004
         id: "client.1eac4f8c-e020-31df-96c",
         phone: "290.757.1167 x",
         firstName: "Yadira",
         lastName: "Dietrich",
         company: "",
         email: "lisa03@example.org",
         city_id: 1,
         address: """
           9202 Joaquin Court\n
           New Hattiebury, TN 90934
           """,
         isCompany: 0,
         status: 1,
         state: "active",
         role: "client",
         created_at: "2017-11-05 11:59:49",
         updated_at: "2017-11-05 11:59:49",
         deleted_at: null,
       },
       App\Profile {#1003
         id: "client.791724a8-18f5-3060-bb6",
         phone: "(989) 841-4920",
         firstName: "Ashleigh",
         lastName: "Beahan",
         company: "",
         email: "pierre04@example.net",
         city_id: 1,
         address: """
           94164 Ross Meadow\n
           Port Helmerside, PA 44559-1028
           """,
         isCompany: 0,
         status: 1,
         state: "active",
         role: "client",
         created_at: "2017-11-05 11:59:49",
         updated_at: "2017-11-05 11:59:49",
         deleted_at: null,
       },
       App\Profile {#982
         id: "client.adb2e488-c980-3fa6-a82",
         phone: "775.719.8987",
         firstName: "Raoul",
         lastName: "Buckridge",
         company: "",
         email: "sydnee32@example.net",
         city_id: 1,
         address: """
           299 Kianna Dam\n
           Port Thea, VT 49661-6377
           """,
         isCompany: 0,
         status: 1,
         state: "active",
         role: "client",
         created_at: "2017-11-05 11:59:49",
         updated_at: "2017-11-05 11:59:49",
         deleted_at: null,
       },
       App\Profile {#981
         id: "client.af2a8c1f-da5c-3e50-8f8",
         phone: "1-682-709-5461",
         firstName: "Alexandre",
         lastName: "Koelpin",
         company: "",
         email: "prohaska.jerry@example.net",
         city_id: 1,
         address: """
           55646 Blick Oval Suite 052\n
           Remingtonberg, TN 24963-8386
           """,
         isCompany: 0,
         status: 1,
         state: "active",
         role: "client",
         created_at: "2017-11-05 11:59:49",
         updated_at: "2017-11-05 11:59:49",
         deleted_at: null,
       },
     ],
   }



$p->pluck('firstName')
=> Illuminate\Support\Collection {#1018
     all: [
       "Gabriella",
       "Yadira",
       "Ashleigh",
       "Raoul",
       "Alexandre",
     ],
   }

 $p->pluck('id')
=> Illuminate\Support\Collection {#1009
     all: [
       0,
       0,
       0,
       0,
       0,
     ],
   }

2 个答案:

答案 0 :(得分:7)

在您的模型中,设置标志:

public $incrementing = false;

默认情况下,Laravel希望主键为数字值并将其转换为int

编辑:

正如MarcinNabiałek所说,设定:

protected $keyType = 'string';

也会有所帮助。

答案 1 :(得分:4)

我相信你应该改变:

protected $keyType = 'int';

为:

protected $keyType = 'string';

(但我还没有对其进行测试,因为我几乎总是使用自动增量整数主键)