Search code examples
laravelcasting

laravel get data without applying casts


I am doing

Model::get()->toArray() 

to get all the data from my table but the model has a cast on the dates.

protected $casts = ['date' => 'datetime:D, M d Y'];

I want to be able to get all the data without applying the cast and just the original datetime format. Is there a way to control when the cast is applied.


Solution

  • You can get all attributes as they are, by using

    Model::get()->transform(function ($item) {
        return $item->getOriginal();
    }))->toArray();
    

    Also can use getOriginal() as

    $model->getOriginal('created_at') 
    

    on any model to get the original value whenever it's needed.

    Note : getOriginal() will include all the $hidden attributes of the model.