Laravel Route Model Binding: Implicit, Explicit & Scoped
Laravel route model binding is the reason a resource controller rarely contains a manual Post::findOrFail($id) call. Part 1 covered how Laravel routing works at a basic level, and ended on the {post} parameter in a resource route resolving straight to a Post model instead of a raw ID. This part covers how that actually happens, and the handful of places it stops working the way you'd expect:…
Laravel route model binding automates the process of retrieving Eloquent models based on URL parameters, eliminating the need for manual lookups. Implicit binding matches the route parameter name to the type-hinted model variable, resolving the model automatically. For custom lookup columns or soft-deleted rows, binding can be scoped to specific columns or enabled with the withTrashed() method.
Scoped bindings allow nested resources to properly associate related records, preventing unauthorized access to other users' data. Explicit binding provides a centralized solution for custom resolution logic applicable across multiple routes. Soft-deleted models are excluded by default but can be included using the withTrashed() method, though care must be taken to ensure compatibility when combining scoped and trashed bindings on parent/child model pairs.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.