What does your OrganizationScope() look like?
I have something similar going on.
I created a listener that listens for an authenticated event: I have the below code in my EventServiceProvider.
protected $listen = [
Authenticated::class => [
RegisterTenantScope::class,
],
];
Then my RegisterTenantScope is my listener so this fires whenever someone is authenticated
public function handle($event)
{
session()->put('tenant_id', auth()->user()->current_tenant_id);
$this->getModels()->each(function($model) {
$model::addGlobalScope(new TenantScope);
$model::creating(function ($model) {
if($model->getTable() == 'users') {
$model->current_tenant_id = session('tenant_id');
} elseif($model->getTable() !== 'tenants' && $model->getTable() !== 'roles') {
$model->tenant_id = session('tenant_id');
}
});
});
}
The getModels() method essentially returns all my models in my models directory. This listener applies the global scope to all my models
This ensures the scope gets applied after someone is authenticated. I do have a middleware, but that is not where I am adding the global scope. The middleware just sets some variables based on the authenticated user as I am using Spatie Permission with the teams feature. https://spatie.be/docs/laravel-permission/v5/basic-usage/teams-permissions