Laravel Eloquent 模型 id 作为字符串返回错误的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38463624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Eloquent model id as string return wrong value in
提问by Margus Pala
There is OauthClient model which $client->id does not return correct value in Laravel 5.2.
在 Laravel 5.2 中有 $client->id 没有返回正确值的 OauthClient 模型。
"lucadegasperi/oauth2-server-laravel": "^5.1" is used that has in migration $table->string('id', 40)->primary();
"lucadegasperi/oauth2-server-laravel": "^5.1" 用于迁移 $table->string('id', 40)->primary();
However when use $client->id in blade template then I get back "0" . Using dd($client) show correct value in id attribute.
但是,当在刀片模板中使用 $client->id 时,我会返回 "0" 。使用 dd($client) 在 id 属性中显示正确的值。
What could be wrong?
可能有什么问题?
Model output from dd()
dd() 的模型输出
OauthClient {#254 ▼
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:7 [▼
"id" => "wISw4JmlMQCCrMupjojcuDTK3k4hwtkb"
"secret" => "nnw3CPTzeQSIfT4KpR0d3XzWIKKoghB3"
"name" => "http://example.com"
"package" => "free"
"user_id" => 2
"created_at" => "2016-07-19 15:36:28"
"updated_at" => "2016-07-19 15:36:28"
]
#original: array:7 [?]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▼
0 => "*"
]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
Table structure from "show create table oauth_clients"
“show create table oauth_clients”中的表结构
CREATE TABLE `oauth_clients` (
`id` varchar(40) COLLATE utf8_unicode_ci NOT NULL,
`secret` varchar(40) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`package` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`user_id` int(10) unsigned NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `oauth_clients_id_secret_unique` (`id`,`secret`),
KEY `oauth_clients_user_id_foreign` (`user_id`),
CONSTRAINT `oauth_clients_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
回答by ruuter
This is because by default the primary key is casted as int
unless explicitly stated otherwise.
这是因为默认情况下,int
除非另有明确说明,否则主键将被强制转换。
(int) "wISw4JmlMQCCrMupjojcuDTK3k4hwtkb" == 0
The value still exists as string, but if you use the $model->id
it will go through magic __get()
method defined in Illuminate\Database\Eloquent\Model
class.
该值仍然以字符串形式存在,但如果您使用$model->id
它,它将通过类中__get()
定义的魔术方法Illuminate\Database\Eloquent\Model
。
I'm not going to argue against using id
field as string, but if you do and also want to get the string value using $model->id, you'll have to cast it as string in you model definition. There is a protected $casts array you can use for that. Just add the following to your OauthClient model:
我不会反对将id
字段用作字符串,但如果您这样做并且还想使用 $model->id 获取字符串值,则必须在模型定义中将其转换为字符串。您可以使用一个受保护的 $casts 数组。只需将以下内容添加到您的 OauthClient 模型中:
protected $casts = ['id' => 'string'];
This will do the trick and cast the id attribute as string instead of default integer. All though I would reccommend not to use id as string in the first place.
这将起作用并将 id 属性转换为字符串而不是默认整数。尽管我建议首先不要将 id 用作字符串。
Update:
更新:
There is also an $incrementing
property, which you can set to false
on your models, to tell Laravel you want non-incrementing or non-numeric primary key. Set it on your models like this:
还有一个$incrementing
属性,你可以false
在你的模型上设置,告诉 Laravel 你想要非递增或非数字主键。像这样在你的模型上设置它:
public $incrementing = false;
Update 2:
更新 2:
Information about this is now also added to official documentation. Look for Primary Keyssection at Eloquent docs:
关于这方面的信息现在也添加到官方文档中。在Eloquent docs 中查找Primary Keys部分:
In addition, Eloquent assumes that the primary key is an incrementing integer value, which means that by default the primary key will be cast to an int automatically. If you wish to use a non-incrementing or a non-numeric primary key you must set the public $incrementing property on your model to false. If your primary key is not an integer, you should set the protected $keyType property on your model to string.
此外,Eloquent 假设主键是一个递增的整数值,这意味着默认情况下主键会自动转换为 int。如果您希望使用非递增或非数字主键,则必须将模型上的公共 $incrementing 属性设置为 false。如果您的主键不是整数,则应将模型上的 protected $keyType 属性设置为字符串。
回答by act28
Setting public $incrementing = false;
worked for me.
设置public $incrementing = false;
对我有用。