Modernizing Your Stack with Laravel 8.70: Enums and Enhanced Scoping
Overview
Laravel 8.70 represents a pivotal update focusing on developer ergonomics and forward compatibility with
Prerequisites
To follow this guide, you should have a solid grasp of
Key Libraries & Tools
- Laravel: The primary PHP framework providing these updates.
- PHP 8.1: The runtime required for native Enum support and modern exception handling.
- Laravel Vapor: A serverless deployment platform now supporting RDS proxies forMySQL 8.
Native Enum Integration
One of the most powerful updates is the ability to cast model attributes directly to
// In your Model
protected $casts = [
'status' => StatusEnum::class,
];
// Usage in queries
$services = Service::where('status', StatusEnum::Done)->get();
Laravel handles the serialization behind the scenes, converting the Enum case to its backed value (string or integer) before it hits the database. This pattern makes your business logic significantly more readable and less prone to typos.
Advanced Route Scoping
Previously, scoping child models in a route required custom keys. The new scopeBindings() method automates this process using your Eloquent relationships.
Route::get('/users/{user}/servers/{server}', function (User $user, Server $server) {
return $server;
})->scopeBindings();
This ensures the {server} must belong to the {user}. If the relationship doesn't exist, Laravel automatically returns a 404, saving you from writing manual ownership checks in your controllers.
Syntax Notes
- Declined Rule: Use
declinedfor negative boolean logic (e.g., verifying a user checked "No" for a criminal record). - Multiple Date Formats: The
date_formatrule now accepts comma-separated strings (e.g.,date_format:Y-m-d,d/m/Y). - Middleware Exclusion: Use
withoutMiddleware()to punch holes in group-level security for specific endpoints like public help pages.
Tips & Gotchas
Always define your Eloquent relationships before using scopeBindings(). If the relationship is missing, the framework will throw an exception rather than a 404. For
