Current Location: Home> Latest Articles> Laravel Field Formatting and Model Field Type Conversion Methods

Laravel Field Formatting and Model Field Type Conversion Methods

gitbox 2025-07-29

What is Field Formatting

Field formatting refers to converting or processing fields in a database table to match specific formatting requirements. In Laravel, we can achieve field formatting through model (Model) field type conversion methods.

Field Type Conversion Methods

Field Type Conversion

Laravel provides a powerful $casts property that allows you to convert model fields to a specified data type. By defining the $casts property in the model, you can automatically convert fields in the database to the required data type.


$casts = [
    'birthday' => 'date:Y-m-d',
    'enabled' => 'boolean',
];

// Using the converted fields
echo $model->birthday;  // Outputs 2022-01-01
echo $model->enabled;   // Outputs true

In the example above, the birthday field is converted to the date format 'Y-m-d', and the enabled field is converted to a boolean type.

Custom Field Conversion

Besides using the default $casts property for type conversion, Laravel also allows custom field conversion through accessors and mutators for more complex transformations.

Accessors

Accessors allow us to perform custom operations when retrieving field values. For example, we can format a date field to a specific string format.


public function getBirthdayAttribute($value)
{
    return date('Y年m月d日', strtotime($value));
}

// Using the accessor
echo $model->birthday;   // Outputs 2022年01月01日

In the code above, the getBirthdayAttribute method formats the birthday field as 'Y年m月d日'.

Mutators

Mutators allow us to modify a field value before saving it to the database. For instance, we can convert a string date format to a standard database date format.


public function setBirthdayAttribute($value)
{
    $this->attributes['birthday'] = date('Y-m-d', strtotime($value));
}

// Using the mutator
$model->birthday = '2022年01月01日';
$model->save();

In the example above, the setBirthdayAttribute method converts the birthday field value to the 'Y-m-d' format before saving it to the database.

Field Formatting Use Cases

Date Formatting

In real-world development, it is common to need to format date fields to meet project-specific requirements. Laravel provides a convenient way to convert date fields.


$casts = [
    'birthday' => 'date:Y-m-d',
];

public function getBirthdayAttribute($value)
{
    return date('Y年m月d日', strtotime($value));
}

public function setBirthdayAttribute($value)
{
    $this->attributes['birthday'] = date('Y-m-d', strtotime($value));
}

In the above code, the birthday field is first converted to the 'Y-m-d' date format, and both an accessor and mutator are defined for date formatting.

Boolean Type Conversion

In a database, certain fields might be defined as integers (0 or 1) to represent boolean values. However, in business logic, it is often more convenient to work directly with boolean values. This can be achieved by setting the Casts type to boolean for that field.


$casts = [
    'enabled' => 'boolean',
];

The above code automatically converts the enabled field from an integer value (0 or 1) to a boolean value (true or false).

Other Field Conversions

In addition to date and boolean type formatting, accessors and mutators can also be used to format other fields. For example, converting numeric fields to currency format or percentage format.


public function getAmountAttribute($value)
{
    return number_format($value, 2);
}

public function setAmountAttribute($value)
{
    $this->attributes['amount'] = str_replace(',', '', $value);
}

In the above code, we use the accessor to format the amount field as a currency value (with two decimal places), and the mutator to convert the formatted value back into a normal numeric value.

Conclusion

Field formatting is an essential feature in Laravel, allowing us to handle and display data according to the project's requirements. With the field type conversion methods discussed in this article, we can easily format model fields, whether it’s for date formatting, boolean type conversion, or other custom transformations. Mastering these techniques will help you handle data more efficiently and flexibly during development.