Mastering the Quality of Life Updates in Laravel v8.55.0
Overview
Prerequisites
To follow this guide, you should have a solid grasp of the
Key Libraries & Tools
- Laravel Framework: The core PHPframework providing the updated features.
- OpenSSL: Underlying library used for the new AES-GCM encryption ciphers.
- Eloquent ORM: Used for handling Soft Deletes and database interactions.
Code Walkthrough
1. Modernizing Encryption with GCM
config/app.php:
'cipher' => 'AES-256-GCM',
2. Streamlined Rate Limiting
The new attempt() method on the RateLimiter facade replaces manual counter checks with a single callback-based execution.
RateLimiter::attempt(
'send-message:'.$user->id,
$maxAttempts = 5,
function() {
// Logic to execute if limit not reached
},
$decaySeconds = 60
);
3. Route-Model Binding with Soft Deletes
You can now easily retrieve models that have been soft-deleted by chaining withTrashed() directly onto your route definition.
Route::get('/profile/{user}', function (User $user) {
return $user;
})->withTrashed();
4. Advanced Validation Features
The Rule::when() method allows for cleaner conditional logic within your validation arrays, while the safe() method provides a fluent interface for interacting with validated data.
$validator = Validator::make($data, [
'seats' => [
'integer',
Rule::when($isBasePlan, ['max:100']),
],
]);
$validated = $validator->safe()->only(['seats']);
Syntax Notes
Notice the shift toward fluent interfaces. Methods like withTrashed() and withoutTrashed() allow developers to describe database constraints in plain English. The Rule::when() method accepts a boolean or a closure, providing flexibility for dynamic validation scenarios without messy if statements.
Practical Examples
- API Throttling: Use the
attempt()method to wrap sensitive API endpoints, ensuring users don't exceed rate limits while keeping the controller code clean. - Administrative Dashboards: Use
withTrashed()in routes to allow administrators to view or restore deleted records via the same URL structure used for active records.
Tips & Gotchas
When switching to GCM encryption, you must decrypt your existing data with the old cipher before re-encrypting it with the new one. Failure to do so will result in DecryptException errors across your application. Always verify your encryption key is set correctly before performing a mass migration of encrypted data.
