Define PHP Backed Enums on model $casts to automatically hydrate strings and integers into typed Enums.
Storing status strings like 'active' or 'pending' as raw strings leads to typos. Defining Backed Enums inside model $casts makes sure typed enum object hydration.
namespace App\Models;
use App\Enums\PostStatus;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected function casts(): array
{
return [
'status' => PostStatus::class,
];
}
}
- Automatically casts database scalar values into typed Backed Enum objects
- Provides full type safety and IDE auto-completion on model attributes
- Throws ValueError when database contains unmapped enum scalar values
Related Tips
View all tips →Cast Enums Inside JSON and Array Column Keys in Eloquent
Cast enum instances nested within JSON or array database columns using Eloquent Attribute accessors and mutators.
Refresh and Pessimistically Lock Models with refreshForUpdate() in Laravel 13.27
Laravel 13.27 adds refreshForUpdate(), reloading an existing model instance in-place with fresh database attributes while acquiring an exclusive FOR UPDATE row lock.