Scoping Resource Routes
To know more about creating resource controller visit our Bajarangisoft site.
Sometimes, when implicitly binding multiple Eloquent models in resource route definitions, you may wish to scope the second Eloquent model such that it must be a child of the first Eloquent model. For example, consider this situation that retrieves a blog post by slug for a specific user:
use App\Http\Controllers\PostsController;
Route::resource('users.posts', PostsController::class)->scoped();
scoped
method:
use App\Http\Controllers\PostsController;
Route::resource('users.posts', PostsController::class)->scoped([
'post' => 'slug',
]);
User
model has a relationship named posts
(the plural of the route parameter name) which can be used to retrieve the Post
model.