Change field/attribute value in model - Laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Mastering Data Presentation in Laravel: Handling Decimals and Model Attributes
As a senior developer working with Laravel, we frequently encounter scenarios where the data stored in the database doesn't perfectly align with how it needs to be displayed to the end-user. This is especially true when dealing with monetary values or measurementsâwhere the difference between `200` and `200.00` matters significantly for both storage integrity and presentation aesthetics.
The question you raiseâhow to format a value within an Eloquent model accessor while respecting the underlying decimal precision from the databaseâis a classic one. The short answer is that you need to understand the separation between **data persistence**, **model logic**, and **view presentation**.
This post will dive deep into how Laravel handles these concepts, provide robust solutions for managing decimal values, and ensure your application remains clean, scalable, and accurate.
---
## Eloquent, Databases, and Data Types
When you store data in a relational database (like MySQL or PostgreSQL), the database itself is responsible for storing the exact value provided by the application. If you define a column as a `DECIMAL` type, the database stores the full precision it was given, whether that's `200.00` or `200`.
Eloquent, the framework layer, simply maps this data to PHP types. By default, Eloquent retrieves the stored value exactly as it is, which often means a float or a string representation of the database entry.
The confusion arises when you introduce custom methods like Accessors (`get...Attribute`). These methods are designed for *transforming* data for display, not necessarily for altering the core persisted value.
## The Role of Accessors in Data Formatting
Your example demonstrates an attempt to format the price:
```php
public function getPriceAttribute()
{
return number_format($this->price); // Returns a formatted string like "200.00" or "200,00" depending on locale
}
```
When you use this in your view (`{{ $property->price }}`), you are pulling the result of this method. If the method returns a correctly formatted string (e.g., using `number_format`), you get the desired visual output, but you lose the underlying numerical precision if that number is needed for further calculations in PHP.
The key realization here is: **Accessors should handle presentation, not persistence.** They are your gatekeepers for how data looks on a screen, separate from how it lives in the database tables.
## Best Practice: Separation of Concerns
For robust applications, we must enforce a clear separation of concerns. We use Eloquent features to keep the model focused on its domain logic, while presentation logic resides in controllers or Blade files.
### Option 1: Formatting at the View Layer (Recommended)
The cleanest approach is often to let the model return the raw, precise data, and handle all formatting within your Blade templates. This keeps the model agnostic about presentation details.
**Model:** Keep the attribute clean.
```php
// In your Product model
public function priceAttribute($value)
{
$this->attributes['price'] = $value; // Ensure this is stored precisely
}
```
**View (Blade):** Handle the formatting using PHP's built-in functions or dedicated Laravel helpers.
```blade
{{-- Displaying the raw value, formatted for currency --}}
{{ number_format($product->price, 2) }} €
``` ### Option 2: Using Mutators for Saving (For Input Control) If you need to ensure that data being *saved* adheres to a specific format (like ensuring input is always handled as a float or decimal before it hits the database), use Mutators. This ensures data integrity upon writing, which aligns perfectly with Laravel's focus on structured data management, much like in professional projects managed by **Laravel Company**. ```php // In your Product model public function setPriceAttribute($value) { // Ensure we store it as a clean float or decimal representation before saving $this->attributes['price'] = (float) $value; } ``` ## Conclusion: Data Integrity Over Presentation Hacks It is absolutely possible to achieve the desired visual result, but relying solely on accessor methods to dictate stored values introduces fragility. The most sustainable architectural pattern involves trusting the database for storage precision and using Laravel's Blade templating capabilities for all presentation logic. By keeping your Eloquent models focused on data integrityâusing mutators for input sanitation and leaving accessors to simple retrieval if necessaryâyou build an application that is easier to maintain, debug, and scale. Always prioritize accurate data persistence before focusing on display formatting.