get Original Attribute for Eloquent Model Laravel 5.1

get Original Attribute for Eloquent Model Laravel 5.1

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Accessing Original Attributes in Laravel Accessors

When building a Laravel application, it is common to transform data before it reaches the view layer. You might format dates, sanitize strings, or calculate derived values using accessors like getFooAttribute. This pattern keeps your models clean and ensures that every time you access $model->foo, the transformation happens automatically. However, this abstraction creates a specific pain point: retrieving the raw, unmodified value of an attribute within other parts of your application.

If you rely solely on the accessor, any logic inside getFooAttribute becomes invisible to the rest of your codebase. You end up with two sources of truth for the same piece of data—the property and the accessor—which can lead to inconsistencies if one changes. The solution isn't to abandon accessors, but rather to decouple the presentation logic from the storage logic.

The most robust approach is to store the original value in the model itself, either directly as a private property or via a setter method, while keeping the accessor focused purely on formatting for output. By defining the raw data at the point of assignment, you ensure that the "original" state is always available regardless of how many times the getter is called.

Consider a scenario where you are calculating taxes or generating reports. You need the base price, not the price formatted with currency symbols or rounded to two decimal places inside the accessor. If your model looks like this:

class Product extends Model
{
    protected $fillable = ['name', 'price'];

    public function getFormattedPriceAttribute()
    {
        return '$' . number_format($this->attributes['price'], 2);
    }
}

You cannot access the raw price simply by referencing $model->formatted_price because that property doesn't exist; only the getter does. To fix this without adding a separate setFormattedPriceAttribute, you should ensure the raw data is accessible via the standard $attributes array or a dedicated private property.

The cleanest implementation involves keeping the raw value in the $attributes array and ensuring your accessor reads from that specific key rather than the public property. However, if you want to access the "original" value of an attribute that has already been cast or mutated, you often need to look at how the data enters the model.

If you are using Laravel 5.1 specifically, you must be aware of how attribute casting interacts with accessors. In earlier versions, accessing a $casted attribute would trigger the accessor logic. To bypass this and get the raw DB value directly from the database without running any accessors or mutators, you should access the underlying attributes array.

$rawPrice = $product->attributes['price'];
// Or if using magic getter on the attributes array
$rawPrice = $product->getAttributeValue('price');

In many cases, getAttributeValue might still trigger accessors depending on how you define them in newer Laravel versions, but in 5.1, accessing the $attributes array directly is the most reliable way to bypass the accessor chain entirely. This gives you direct access to the raw data stored in the model instance.

Alternatively, if you require the original value frequently, consider defining a private property on the model that holds the unmodified data. You can populate this property via a setter or a boot event when the model is instantiated from the database. This creates an explicit separation between the storage layer (raw data) and the presentation layer (formatted output).

The trade-off here is increased complexity in the model class. You are managing two values for the same concept: one for display and one for logic. While this adds a small amount of boilerplate, it guarantees that your business logic never accidentally relies on a string representation of a number or a formatted date. This distinction is crucial when performing calculations or persisting data back to the database, ensuring you are always working with the canonical source of truth.

For further reading on advanced model patterns and attribute handling in Laravel, check out resources from https://laravelcompany.com.

Our network