Skip to content

The casting for appended attributes (accessors) doesn't work #29271

@vovchok

Description

@vovchok
  • 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:

  1. 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;
    }
}
  1. Create an instance of a model and call toArray or toJson 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions