-
Notifications
You must be signed in to change notification settings - Fork 11.6k
Closed
Description
- Laravel Version: 5.8.29
- PHP Version: 7.2.18
Description:
The casting for appended attributes (accessors) at a model doesn't work when trying to get an array or JSON representations.
Steps To Reproduce:
- Create a model with appended accessors and casting declaration.
class EloquentModelAppendsWithCastingStub extends Model
{
protected $casts = [
'is_admin' => 'int',
];
protected $appends = ['is_admin'];
public function getIsAdminAttribute()
{
return true;
}
}
- Create an instance of a model and call
toArray
ortoJson
methods:
>>> $model = new \App\EloquentModelAppendsWithCastingStub();
=> App\EloquentModelAppendsWithCastingStub {#3009}
>>> $model->toArray();
=> [
"is_admin" => true,
]
>>> $model->toJson();
=> "{"is_admin":true}"
>>>
Expected results:
>>> $model = new \App\EloquentModelAppendsWithCastingStub();
=> App\EloquentModelAppendsWithCastingStub {#3009}
>>> $model->toArray();
=> [
"is_admin" => 1,
]
>>> $model->toJson();
=> "{"is_admin":1}"
>>>
Metadata
Metadata
Assignees
Labels
No labels