company_id)) { $companyId = session('current_company_id'); if (! $companyId && Auth::check()) { $companyId = Auth::user()->currentCompany->id; session(['current_company_id' => $companyId]); } // Special handling for notifications in job context if (! $companyId && $model->getTable() === 'notifications' && isset($model->notifiable_id)) { // Get company_id from the notifiable user's current_company_id $user = User::find($model->notifiable_id); if ($user?->current_company_id) { $companyId = $user->current_company_id; } } if ($companyId) { $model->company_id = $companyId; } else { Log::error('CurrentCompanyScope: No company_id found for user ' . Auth::id()); throw new ModelNotFoundException('CurrentCompanyScope: No company_id set in the session, user, or database.'); } } }); static::addGlobalScope(new CurrentCompanyScope); } public function company(): BelongsTo { return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id'); } }