Laravel provides a powerful way to manipulate model attributes using accessors and mutators. These allow you to format or transform data when retrieving or saving it to the database. With Laravel 12, the syntax for defining accessors and mutators has been updated to make it more concise and intuitive.
This article will explore how to format dates using accessors and mutators with the new syntax introduced in Laravel 12.
What Are Accessors and Mutators?
- Accessors: Modify how an attribute is retrieved from the database.
- Mutators: Modify how an attribute is stored in the database.
For example, you can use an accessor to format a date when retrieving it and a mutator to ensure the date is stored in a specific format.
Formatting Dates with Accessors
Let’s say you have a `User` model with a `birth_date` column. When retrieving it, you want to format the date as `d-m-Y` (e.g., `25-12-2023`).
Here’s how you can define an accessor in Laravel 12:
// filepath: /app/Models/User.php
use Illuminate\Database\Eloquent\Casts\Attribute;
class User extends Model
{
// ...existing code...
protected function birthDate(): Attribute
{
return Attribute::make(
get: fn ($value) => \Carbon\Carbon::parse($value)->format('d-m-Y'),
);
}
}
Explanation:
- The `birthDate` method defines an accessor for the `birth_date` attribute.
- The `get` callback formats the raw database value using Carbon’s `format()` method.
Now, whenever you access `$user->birth_date`, it will return the formatted date.
Storing Dates with Mutators
If you want to ensure that dates are stored in the database in the `Y-m-d` format, you can define a mutator like this:
// filepath: /app/Models/User.php
use Illuminate\Database\Eloquent\Casts\Attribute;
class User extends Model
{
// ...existing code...
protected function birthDate(): Attribute
{
return Attribute::make(
// ...existing accessor code...
set: fn ($value) => \Carbon\Carbon::createFromFormat('d-m-Y', $value)->format('Y-m-d'),
);
}
}
Explanation:
The `set` callback ensures that the input value (e.g., `25-12-2023`) is converted to the `Y-m-d` format before being saved to the database.
Now, when you assign a value to `$user->birth_date`, it will automatically be formatted correctly for storage.
Full Example
Here’s how you can use both the accessor and mutator together:
// filepath: /app/Models/User.php
use Illuminate\Database\Eloquent\Casts\Attribute;
class User extends Model
{
// ...existing code...
protected function birthDate(): Attribute
{
return Attribute::make(
get: fn ($value) => \Carbon\Carbon::parse($value)->format('d-m-Y'),
set: fn ($value) => \Carbon\Carbon::createFromFormat('d-m-Y', $value)->format('Y-m-d'),
);
}
}
Usage:
$user = new User();
// Setting the date (mutator in action)
$user->birth_date = '25-12-2023';
$user->save(); // Stored as '2023-12-25' in the database
// Retrieving the date (accessor in action)
echo $user->birth_date; // Outputs '25-12-2023'
Benefits of the New Syntax in Laravel 12
- Conciseness: The new `Attribute::make()` method combines both accessors and mutators into a single method, reducing boilerplate code.
- Readability: The `get` and `set` callbacks make it clear how the attribute is being transformed.
- Flexibility: You can define either an accessor, a mutator, or both, depending on your needs.
Conclusion
Accessors and mutators are powerful tools for transforming model attributes in Laravel. With the new syntax introduced in Laravel 12, defining them is now more concise and intuitive. By using accessors and mutators, you can ensure consistent formatting of dates (or any other attributes) across your application.
Start using this feature today to simplify your code and improve data handling in your Laravel projects!
Leave a Reply