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;
}
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;
}
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.
Please or to participate in this conversation.