Mar*_*ala 8 php oauth laravel eloquent laravel-5
有一个OauthClient模型,$ client-> id在Laravel 5.2中没有返回正确的值.
"lucadegasperi/oauth2-server-laravel":"^ 5.1"用于迁移$ table-> string('id',40) - > primary();
但是当在刀片模板中使用$ client-> id时,我会返回"0".使用dd($ client)在id属性中显示正确的值.
可能有什么不对?
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
}
Run Code Online (Sandbox Code Playgroud)
"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
Run Code Online (Sandbox Code Playgroud)
ruu*_*ter 21
这是因为int除非另有明确说明,否则默认情况下主键是按原样输出的.
(int) "wISw4JmlMQCCrMupjojcuDTK3k4hwtkb" == 0
Run Code Online (Sandbox Code Playgroud)
该值仍然以字符串形式存在,但如果您使用$model->id它将通过类中__get()定义的魔术方法Illuminate\Database\Eloquent\Model.
我不打算反对使用id字段作为字符串,但如果你这样做并且也希望使用$ model-> id获取字符串值,则必须在模型定义中将其转换为字符串.您可以使用受保护的$ casts数组.只需将以下内容添加到您的OauthClient模型中:
protected $casts = ['id' => 'string'];
Run Code Online (Sandbox Code Playgroud)
这将执行技巧并将id属性转换为字符串而不是默认整数.虽然我会建议不要首先使用id作为字符串.
更新:
还有一个$incrementing属性,您可以false在模型上设置该属性,以告诉Laravel您想要非递增或非数字主键.将它设置在您的模型上,如下所示:
public $incrementing = false;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7718 次 |
| 最近记录: |