checkAuthority(); } public function rules() { /** @var \App\Models\User $user */ $user = auth()->user(); $rules = [ 'entity' => 'required|bail|in:invoices,quotes,credits,recurring_invoices,clients,vendors,credits,payments,projects,tasks,expenses,recurring_expenses,bank_transactions,purchase_orders', 'entity_id' => ['required','bail', Rule::exists($this->entity, 'id')->where('company_id', $user->company()->id)], 'notes' => 'required|bail', ]; return $rules; } public function prepareForValidation() { $input = $this->all(); if(isset($input['entity_id']) && $input['entity_id'] != null) { $input['entity_id'] = $this->decodePrimaryKey($input['entity_id']); } $this->replace($input); } public function checkAuthority(): bool { $this->error_message = ctrans('texts.authorization_failure'); /** @var \App\Models\User $user */ $user = auth()->user(); $entity = $this->getEntity(); return $user->isAdmin() || $user->can('view', $entity); } public function getEntity() { if(!$this->entity) { return false; } $class = "\\App\\Models\\".ucfirst(Str::camel(rtrim($this->entity, 's'))); return $class::withTrashed()->find(is_string($this->entity_id) ? $this->decodePrimaryKey($this->entity_id) : $this->entity_id); } }