从 Laravel 5.2 中的 Eloquent 模型检索后变量类型发生变化
Variable type changes after retrieving from Eloquent model in Laravel 5.2
我在数据库中将 table 命名为 marital_status。这仅包含两列 user_id(整数类型)和另一列名为 is_married(布尔类型)。
因此,当我通过 eloquent 模型检索用户的婚姻状况时,我会排除布尔值。
Auth::user()->maritalStatus // equals always string not boolean.
结果正确,但我得到的不是布尔值,而是字符串“0”或“1”。有人可以解释一下吗?
您可以在模型中解决这个问题:
protected $casts = [
'is_married' => 'boolean'
];
https://laravel.com/docs/5.2/eloquent-mutators#attribute-casting
我在数据库中将 table 命名为 marital_status。这仅包含两列 user_id(整数类型)和另一列名为 is_married(布尔类型)。
因此,当我通过 eloquent 模型检索用户的婚姻状况时,我会排除布尔值。
Auth::user()->maritalStatus // equals always string not boolean.
结果正确,但我得到的不是布尔值,而是字符串“0”或“1”。有人可以解释一下吗?
您可以在模型中解决这个问题:
protected $casts = [
'is_married' => 'boolean'
];
https://laravel.com/docs/5.2/eloquent-mutators#attribute-casting