getOriginal() is not working in laravel 8

Viewed 9027

Below is my Eloquent model

namespace App\Models;
    
use App\Models\BaseModel as Model;
use Illuminate\Support\Facades\Storage;
    
class Banner extends Model {
    protected $table = 'banners';
    protected $fillable = ['image', 'isactive', 'type'];
    
    protected $hidden = ['created_at', 'deleted_at', 'updated_at'];
    
    public function getImageAttribute($value) {
        if ($value)
            return Storage::url($value);
        return null;
    }
}

I am using Eloquant statement:

App\Models\Banner::find(2)->getOriginal('image');

It is giving accesor value of image attribute instead of original value

2 Answers

Laravel 7.x change getRawOriginal() instead of getOriginal()...

The $model->getOriginal() method will now respect any casts and mutators defined on the model. Previously, this method returned the uncast, raw attributes. If you would like to continue retrieving the raw, uncast values, you may use the getRawOriginal method instead.

See: https://laravel.com/docs/7.x/upgrade#factory-types

You probably want getRawOriginal to not have the accessor used.

$banner = Banner::findOrFail(2);
$image = $banner->getRawOriginal('image');
// or from the attributes
$image = $banner->getAttributes()['image'] ?? null;
Related