Skip to content

Commit

Permalink
Merge pull request #945 from bakaphp/KA-86
Browse files Browse the repository at this point in the history
fix: count users, companies and modules
  • Loading branch information
FredPeal authored Feb 16, 2024
2 parents a1e8282 + bdc4b69 commit e33f917
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Kanvas/Apps/Models/Apps.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Kanvas\Exceptions\ModelNotFoundException as ExceptionsModelNotFoundException;
use Kanvas\Models\BaseModel;
use Kanvas\Roles\Models\Roles;
use Kanvas\SystemModules\Models\SystemModules;
use Kanvas\Users\Models\UserCompanyApps;
use Kanvas\Users\Models\Users;
use Kanvas\Users\Models\UsersAssociatedApps;
Expand Down Expand Up @@ -98,6 +99,11 @@ public function companies(): HasManyThrough
);
}

public function systemModules(): HasMany
{
return $this->hasMany(SystemModules::class, 'apps_id');
}

public function users(): BelongsToMany
{
return $this->belongsToMany(Users::class, 'users_associated_apps', 'apps_id', 'users_id');
Expand All @@ -108,6 +114,27 @@ public function keys(): HasMany
return $this->hasMany(AppKey::class, 'apps_id');
}

public function getTotalUsersAttribute(): int
{
if (! $totalUser = $this->get('total_users')) {
$this->set('total_users', $this->users()->count());

return $this->get('total_users');
}

return $totalUser;
}

public function getTotalCompaniesAttribute(): int
{
if (! $totalCompanies = $this->get('total_companies')) {
$this->set('total_companies', $this->companies()->count());

return $this->get('total_companies');
}
return $totalCompanies;
}

public function getUserKeys(?UserInterface $user = null): Collection
{
$user = $user ?? Auth::user();
Expand Down

0 comments on commit e33f917

Please sign in to comment.