Laravel attribute casting and accessors/mutators
PHP Dasun Kalupahana 70 views
<?php
use Illuminate\Database\Eloquent\Casts\Attribute;
protected function casts(): array
{
return [
'settings' => 'array',
'published_at' => 'datetime',
'status' => PostStatus::class, // backed enum
];
}
protected function name(): Attribute
{
return Attribute::make(
get: fn ($value) => ucfirst($value),
set: fn ($value) => strtolower($value),
);
}
casts() converts columns to arrays, dates or enums automatically. Attribute::make transforms a value when you read or write it.
Learn more on the blog
MySQL
PostgreSQL vs MySQL Performance: Real Benchmarks Compared
Last updated: 7 November 2026. See the update log at the end."Which...
8 min read
PHP
How to Detect and Fix PHP Memory Leaks
Last updated: 5 November 2026. See the update log at the end.In...
9 min read
Docker
Docker Build Too Slow? 10 Ways to Speed It Up
A slow Docker build usually has one of a few causes: you...
8 min read