Reputation: 588
If I have route like this.
Route::get('/users/{user}/posts/{post}', function (User $user, Post $post) {
return $post;
});
Then If User
id 1 has Post
id 1,2,3 and User
id 2 has Post
id 4,5,6.
How to make Post
that bind in route return 404
. When Post
was not create by User
like /users/1/posts/4
.
Upvotes: 0
Views: 61
Reputation: 588
So the point is I used scopeBindings
chain from Route::resource
. So it didn't work
It have to use this way
Route::scopeBindings()->group(function () {
Route::resource('users.posts', UserPostController::class);
});
It work perfectly.
Upvotes: 1
Reputation: 7992
If you wish, you may instruct Laravel to scope "child" bindings even when a custom key is not provided. To do so, you may invoke the
scopeBindings
method when defining your route:
use App\Models\Post;
use App\Models\User;
Route::get('/users/{user}/posts/{post}', function (User $user, Post $post) {
return $post;
})->scopeBindings();
/users/1/posts/1 <- returns the post 1
/users/1/posts/4 <- returns 404
/users/2/posts/4 <- returns the post 4
/users/2/posts/1 <- returns 404
Upvotes: 1