pluck('id') 将字符串值转换为零
pluck('id') converts string values to zero
我有一个 table,其主键类型为 string
,用于存储 uuid 值。当我查询数据时,一切都很好。但是,当我使用 pluck('id')
获取所有 ID 的数组时,我得到的是零数组!
我试着用 pluck('firstName')
和它的 returns 数组来测试它。当我用 'id'
替换 'firstName'
时,我得到零值数组。
pluck 会自动将 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,
],
}
在您的模型中,设置标志:
public $incrementing = false;
默认情况下,Laravel期望主键是一个数值并将其转换为int
。
编辑:
正如 Marcin Nabiałek 所说,设置:
protected $keyType = 'string';
也会有帮助。
我相信你应该改变:
protected $keyType = 'int';
至:
protected $keyType = 'string';
(但我没有测试过,因为我几乎总是使用自增整数主键)
我有一个 table,其主键类型为 string
,用于存储 uuid 值。当我查询数据时,一切都很好。但是,当我使用 pluck('id')
获取所有 ID 的数组时,我得到的是零数组!
我试着用 pluck('firstName')
和它的 returns 数组来测试它。当我用 'id'
替换 'firstName'
时,我得到零值数组。
pluck 会自动将 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,
],
}
在您的模型中,设置标志:
public $incrementing = false;
默认情况下,Laravel期望主键是一个数值并将其转换为int
。
编辑:
正如 Marcin Nabiałek 所说,设置:
protected $keyType = 'string';
也会有帮助。
我相信你应该改变:
protected $keyType = 'int';
至:
protected $keyType = 'string';
(但我没有测试过,因为我几乎总是使用自增整数主键)