Laravel雄辩的模型ID作为字符串返回错误的值 [英] Laravel Eloquent model id as string return wrong value in

查看:64
本文介绍了Laravel雄辩的模型ID作为字符串返回错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个OauthClient模型,其中$ client-> id在Laravel 5.2中未返回正确的值.

There is OauthClient model which $client->id does not return correct value in Laravel 5.2.

"lucadegasperi/oauth2-server-laravel":在迁移中使用了"^ 5.1" $ table-> string('id',40)-> primary();

"lucadegasperi/oauth2-server-laravel": "^5.1" is used that has in migration $table->string('id', 40)->primary();

但是,当在刀片模板中使用$ client-> id时,我会返回"0".使用dd($ client)在id属性中显示正确的值.

However when use $client->id in blade template then I get back "0" . Using dd($client) show correct value in id attribute.

有什么问题吗?

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
}

显示创建表oauth_clients"中的表结构

Table structure from "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

推荐答案

这是因为默认情况下,除非另有明确说明,否则主键将强制转换为int.

This is because by default the primary key is casted as int unless explicitly stated otherwise.

(int) "wISw4JmlMQCCrMupjojcuDTK3k4hwtkb" == 0

该值仍然以字符串形式存在,但是如果使用$model->id,它将通过Illuminate\Database\Eloquent\Model类中定义的魔术__get()方法.

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.

我不会反对使用id字段作为字符串,但是如果您这样做并且还想使用$ model-> id获取字符串值,则必须在模型中将其转换为字符串定义.有一个受保护的$ casts数组可用于此目的.只需将以下内容添加到您的OauthClient模型中即可:

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:

protected $casts = ['id' => 'string'];

这可以解决问题,并将id属性转换为字符串而不是默认整数.尽管我建议不要一开始就将id用作字符串.

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.

更新:

还有一个$incrementing属性,您可以在模型上将其设置为false,以告诉Laravel您需要非增量或非数字主键.像这样在您的模型上设置它:

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:

public $incrementing = false;

更新2:

有关此的信息现在也已添加到官方文档中. 在雄辩的文档中查找主键:

Information about this is now also added to official documentation. Look for Primary Keys section at Eloquent docs:

此外,Eloquent假定主键是增量键 整数值,这意味着默认情况下,主键为 自动转换为int.如果您希望使用非增量或 非数字主键,您必须设置公共$ incrementing 您模型的属性为false.如果您的主键不是 整数,应在模型上设置受保护的$ keyType属性 串起来.

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.

这篇关于Laravel雄辩的模型ID作为字符串返回错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆