如何将 Laravel Eloquent 里的数值属性强制转换为布尔类型?
I wanted to return a value that gets saved as an integer in the 'active' column in a database table as a boolean.
My initial ugly solution was to change the attribute like this (that is to force the return type to boolean):
public function getActiveAttribute($value): bool
{
return $value;
}
My code reviewer found a much better solution:
https://learnku.com/docs/laravel/6.x/eloquent-muta...
So I replaced above code in the Eloquent model with this:
protected $casts = [
'active' => 'boolean',
];
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。