diff --git a/app/Http/Requests/Client/StoreClientRequest.php b/app/Http/Requests/Client/StoreClientRequest.php index 7d1b1ed4f7db..651fd146a2b5 100644 --- a/app/Http/Requests/Client/StoreClientRequest.php +++ b/app/Http/Requests/Client/StoreClientRequest.php @@ -76,7 +76,7 @@ class StoreClientRequest extends Request ]; if (auth()->user()->company()->account->isFreeHostedClient()) { - $rules['hosted_clients'] = new CanStoreClientsRule($this->company_id); + $rules['id'] = new CanStoreClientsRule(auth()->user()->company()->id); } $rules['number'] = ['nullable',Rule::unique('clients')->where('company_id', auth()->user()->company()->id)]; diff --git a/app/Http/ValidationRules/Ninja/CanStoreClientsRule.php b/app/Http/ValidationRules/Ninja/CanStoreClientsRule.php index 23221c042c2b..85f56c31c884 100644 --- a/app/Http/ValidationRules/Ninja/CanStoreClientsRule.php +++ b/app/Http/ValidationRules/Ninja/CanStoreClientsRule.php @@ -21,6 +21,8 @@ class CanStoreClientsRule implements Rule { public $company_id; + public $company; + public function __construct($company_id) { $this->company_id = $company_id; @@ -33,9 +35,9 @@ class CanStoreClientsRule implements Rule */ public function passes($attribute, $value) { - $company = Company::find($this->company_id); + $this->company = Company::find($this->company_id); - return $company->clients->count() < $company->account->hosted_client_count; + return $this->company->clients()->count() < $this->company->account->hosted_client_count; } /** @@ -43,6 +45,6 @@ class CanStoreClientsRule implements Rule */ public function message() { - return ctrans('texts.limit_clients', ['count' => $company->account->hosted_client_count]); + return ctrans('texts.limit_clients', ['count' => $this->company->account->hosted_client_count]); } }