Scoped bindings and without scoped bindings are two different ways of injecting route model bindings in Laravel.
When you define a route with model bindings, Laravel will automatically resolve the route parameters by querying the corresponding models from the database. By default, Laravel uses "without scoped bindings" behavior, which means it will resolve the route parameters based on the primary key of the model.
In the example you provided, the route is defined as follows:
Route::get('/users/{user}/posts/{post}', function (User $user, Post $post) {
return $post;
})->scopeBindings();
The scopeBindings method is used to enable scoped bindings. Scoped bindings allow you to define custom query constraints when resolving the route parameters. This can be useful when you want to add additional conditions to the query.
To use scoped bindings, you need to define a resolveRouteBinding method on your model. This method will be responsible for resolving the model instance based on the route parameter.
Here's an example of how you can define the resolveRouteBinding method on the Post model:
class Post extends Model
{
// ...
public function resolveRouteBinding($value, $field = null)
{
return $this->where('slug', $value)->firstOrFail();
}
}
In this example, the resolveRouteBinding method is defined to resolve the Post model based on the slug column instead of the primary key. This allows you to retrieve the post based on the slug value in the URL.
By using ->scopeBindings(), Laravel will call the resolveRouteBinding method on the Post model to resolve the $post route parameter.
I hope this explanation helps you understand scoped bindings and without scoped bindings in Laravel. Let me know if you have any further questions!