id()) ->where('role_id', Role::COMPANY_ADMIN) ->count() == 1 ) { $companyAdmin = CompanyAdmin::where('user_id', auth()->id())->first(); return $agentsIds = Agent::where('company_id', $companyAdmin->company_id)->pluck('id'); } else { $agent = Agent::where('user_id', auth()->id())->first(); return [$agent->id]; } } private function getContracts() { $contracts = Contract::whereHas('deal', function ($dealSubQuery) { $dealSubQuery->with('deals') ->whereIn('agent_id', $this->getSelectingAgents()); }); $this->appendMode($contracts); $this->appendFilter($contracts); return $contracts->get(); } private function appendMode(&$query) { if ($this->mode == 'active') { $query->whereIn('status', [ ContractStatus::NEW , ContractStatus::RESERVATION, ContractStatus::SUCCESS, ]); } if ($this->mode == 'finished') { $query->whereIn('status', [ ContractStatus::DECLINE, ContractStatus::TREATY ]); } } private function appendFilter(&$query) { foreach ($this->filter as $fKey => $fValue) { $query->where($fKey, $fValue); } } public function render() { return view('contracts::livewire.table.index', [ 'contracts' => $this->getContracts(), 'statuses' => ContractStatus::class ]); } }