Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chrisblackwell's avatar

Using Route Model Binding without Global Scope

I'm a big fan of route model binding, but I have once instance where it's not working for me. If I have a global scope setup to remove inactive users, how can I tell the model binding to ignore scopes?

For example

public function reactivate(User $user)
{
    // $user isn't available because my global scope 
    // has all users with is_active ignored
    $user->is_active = true;
    $user->save();
    return $user;
}
0 likes
5 replies
Yamen's avatar
public function reactivate($id)
{
    $user = User::withoutGlobalScopes()->find($id);
    // $user isn't available because my global scope 
    // has all users with is_active ignored
    $user->is_active = true;
    $user->save();
    return $user;
}
chrisblackwell's avatar

Yes I could do that, but I'm looking for a way to use it with Route model binding

sutherland's avatar

In your RouteServiceProvider you can customize the resolution logic:

Route::bind('user', function ($value) {
    return App\User::withoutGlobalScopes()->find($value);
});
3 likes
Jaytee's avatar

You can also do withoutGlobalScope(MyGlobalScope::class) which as you'd expect, just removes one global scope.

2 likes
thomaskim's avatar
Level 41

I think what Chris is asking is how to remove global scopes for just that one route, not for all routes.

What you can do is change the parameter name in your web file for that one route.

Route::get('users/{any_user}', 'SomeController@reactivate');

Note the any_user. Change that to whatever naming convention you find most convenient.

Then, in your RouteServiceProvider, change your boot method to something like this:

    public function boot()
    {
        parent::boot();

        Route::bind('any_user', function($id) {
            return \App\User::withoutGlobalScopes()->findOrFail($id);
        });
    }

Make sure that the any_user parameter name matches the one in your routes file. Then, customize that logic.

12 likes

Please or to participate in this conversation.