Laravel 10.33 & 10.34 Update: Type Enforcement and Schema Inspection
Overview
Prerequisites
To follow along, you should have a solid grasp of
Key Libraries & Tools
- Laravel Framework (10.x): The primary ecosystem receiving these updates.
- Illuminate\Support\Collection: The core class powering the
ensuremethod. - Schema Facade: The tool used for direct database introspection.
Code Walkthrough
Multi-Type Collection Enforcement
Previously, the ensure method only accepted a single class or type. Now, you can pass an array to validate collections containing mixed types, such as a feed containing both Podcast and News items.
$collection = collect([new Podcast, new News]);
// Now supports multiple types via an array
$collection->ensure([Podcast::class, News::class]);
Global Missing Handlers for Route Groups
Handling 404s for .missing() calls. You can now chain the missing method directly onto a route group.
Route::prefix('news')->missing(function () {
return redirect()->route('welcome');
})->group(function () {
Route::get('/{news}', [NewsController::class, 'show']);
Route::get('/{news}/edit', [NewsController::class, 'edit']);
});
Advanced Schema Inspection
New methods on the Schema facade allow you to retrieve database structures without manual SQL queries.
use Illuminate\Support\Facades\Schema;
$tables = Schema::getTables(); // Returns all table details
$views = Schema::getViews(); // Returns all database views
Syntax Notes
ensure reflects get{Entity} pattern established by the earlier getColumns method.
Practical Examples
Use the multi-type ensure method when building polymorphic activity feeds where only specific model types are allowed. Apply the group missing method in multi-tenant applications to redirect users back to a dashboard if a specific resource ID is no longer valid or accessible.
Tips & Gotchas
When using missing on a group, remember it applies to all routes within that block that use
