Enums have become a cornerstone of modern PHP development since their introduction in PHP 8.1. They provide type safety, eliminate “magic strings,” and make code more expressive. Laravel quickly embraced enums, allowing them in model casts, validation rules, and factories.
But one area remained awkward: translations.
When passing enum values into translation strings, developers had to call ->value to extract the underlying string. This added noise, cluttered code, and made translations less elegant.
With Laravel 12.42, that friction is gone. You can now pass enums directly into translation replacements, and Laravel will automatically resolve them to their string values. It’s a small change, but one that makes your code cleaner, safer, and more expressive.
✨ The Problem Before 12.42
Let’s say you have a string‑backed enum for months:
enum Month: string {
case January = 'January';
case February = 'February';
case March = 'March';
}
And a translation file:
// resources/lang/en/messages.php
return [
'report' => 'The report is for :month.',
];
Old Way (Pre‑12.42)
__('messages.report', ['month' => Month::February->value]);
// Output: "The report is for February."
👉 Notice the ->value call. Without it, Laravel wouldn’t know how to handle the enum. This was repetitive and easy to forget, especially in larger projects.
⚡ The New Way (Laravel 12.42)
Now you can simply pass the enum itself:
__('messages.report', ['month' => Month::February]);
// Output: "The report is for February."
👉 Cleaner, more expressive, and less error‑prone. Laravel automatically resolves the enum to its string value.
🧪 Example 1: Role‑Based Translations
Suppose you have a user role enum:
enum Role: string {
case Admin = 'Administrator';
case Editor = 'Editor';
case Viewer = 'Viewer';
}
And a translation string:
// resources/lang/en/auth.php
return [
'welcome' => 'Welcome, :role!',
];
Using Enums Directly
__('auth.welcome', ['role' => Role::Admin]);
// Output: "Welcome, Administrator!"
👉 No need for Role::Admin->value. This makes role‑based messages much cleaner.
🧪 Example 2: Order Status Updates
Imagine an ecommerce app with an enum for order statuses:
enum OrderStatus: string {
case Pending = 'Pending';
case Shipped = 'Shipped';
case Delivered = 'Delivered';
case Cancelled = 'Cancelled';
}
Translation file:
// resources/lang/en/orders.php
return [
'status' => 'Your order is currently :status.',
];
Cleaner Code
__('orders.status', ['status' => OrderStatus::Shipped]);
// Output: "Your order is currently Shipped."
👉 Perfect for notifications, dashboards, and customer emails.
🧪 Example 3: Multi‑Language Support
Enums in translations shine even more in multilingual apps. Suppose you have translations for months in French:
// resources/lang/fr/messages.php
return [
'report' => 'Le rapport est pour :month.',
];
Now, you can pass the same enum:
App::setLocale('fr');
__('messages.report', ['month' => Month::February]);
// Output: "Le rapport est pour February."
👉 The enum value is still “February,” but you can map enums to localized strings if needed. For example, you could create a helper that maps Month::February to “Février” when the locale is French. The key point is: enums integrate seamlessly into Laravel’s translation system.
📈 Benefits
- Cleaner syntax: Eliminates repetitive
->valuecalls. - Type safety: Enums prevent invalid strings from creeping into translations.
- Consistency: Works across all translation replacements.
- Developer experience: More expressive, Laravel‑style code.
- Reduced errors: No more forgetting
->valueand causing subtle bugs.
🛡 Real‑World Use Cases
- Reports & dashboards: Pass enums like
Month::Februarydirectly into localized strings. - Role‑based messages: Cleaner handling of user roles in multilingual apps.
- Status updates: Enums for order states (
Pending,Shipped,Delivered) can be translated without extra boilerplate. - Notifications: Use enums in email or system messages seamlessly.
- Multi‑tenant SaaS: Enums make translations safer across different locales and environments.
🧩 How It Fits Into Laravel’s Ecosystem
Laravel has steadily improved enum support:
- Validation rules accept enums.
- Models can cast attributes to enums.
- Factories can generate enum values.
- Now translations accept enums directly.
This makes enums a first‑class citizen in Laravel, aligning with the framework’s philosophy of expressive, developer‑friendly code. It also reduces friction for teams adopting enums across their applications.
🎯 Final Thoughts
Laravel 12.42’s support for enums in translations may seem small, but it’s a quality‑of‑life improvement that developers will appreciate daily. By removing the need for ->value calls, Laravel makes code cleaner, safer, and more expressive.
If you’re already using enums in your application, this update is a no‑brainer. Upgrade to Laravel 12.42 and start writing translations that feel natural and elegant.
👉 Cleaner code, fewer bugs, and a smoother developer experience — that’s the Laravel way.
