Laravel Eloquent: Attribute Casting - luannguyenQV/laravel-framework-demo-full-blog GitHub Wiki
What is casting?
Convert attributes to another data type.
When you need casting?
Boolean: when you need to save/retrieve a value as Bool, bool with only 2 values is 0 or 1, you need type casting. When you need add "sex" attribute for User Model, you add:
protected $casts = [ // $cast: is variable from Model
'sex' => 'boolean', // sex is male/female, compatible with 1/0
];
How to apply casting?
And, after add 3 line of code before, you can use:
$user = App\User::find(1);
if ($user->sex) {
}