db); $this->query = $this->documents(); } #[Computed()] public function client() { return Client::withTrashed()->find($this->client_id); } public function render() { $this->updateResources(request()->tab ?: $this->tab); return render('components.livewire.documents-table', [ 'documents' => $this->query ->orderBy($this->sort_field, $this->sort_asc ? 'asc' : 'desc') ->withTrashed() ->paginate($this->per_page), ]); } public function updateResources(string $resource) { $this->tab = $resource; switch ($resource) { case 'documents': $this->query = $this->documents(); break; case 'credits': $this->query = $this->credits(); break; case 'expenses': // $this->query = $this->expenses(); break; case 'invoices': $this->query = $this->invoices(); break; case 'payments': $this->query = $this->payments(); break; case 'projects': $this->query = $this->projects(); break; case 'quotes': $this->query = $this->quotes(); break; case 'recurringInvoices': $this->query = $this->recurringInvoices(); break; case 'tasks': $this->query = $this->tasks(); break; default: $this->query = $this->documents(); break; } } protected function documents() { $client = $this->client(); return $client->documents() ->where('is_public', true) ->orWhere(function ($query) use ($client) { $query->whereHasMorph('documentable', [Company::class], function ($q) use ($client) { $q->where('is_public', true)->where('company_id', $client->company_id); }); }); } protected function credits() { return Document::query() ->where('is_public', true) ->whereHasMorph('documentable', [Credit::class], function ($query) { $query->where('client_id', $this->client()->id); }); } protected function expenses() { return Document::query() ->where('is_public', true) ->whereHasMorph('documentable', [Expense::class], function ($query) { $query->where('client_id', $this->client()->id); }); } protected function invoices() { return Document::query() ->where('is_public', true) ->whereHasMorph('documentable', [Invoice::class], function ($query) { $query->where('client_id', $this->client()->id); }); } protected function payments() { return Document::query() ->where('is_public', true) ->whereHasMorph('documentable', [Payment::class], function ($query) { $query->where('client_id', $this->client()->id); }); } protected function projects() { return Document::query() ->where('is_public', true) ->whereHasMorph('documentable', [Project::class], function ($query) { $query->where('client_id', $this->client()->id); }); } protected function quotes() { return Document::query() ->where('is_public', true) ->whereHasMorph('documentable', [Quote::class], function ($query) { $query->where('client_id', $this->client()->id); }); } protected function recurringInvoices() { return Document::query() ->where('is_public', true) ->whereHasMorph('documentable', [RecurringInvoice::class], function ($query) { $query->where('client_id', $this->client()->id); }); } protected function tasks() { return Document::query() ->where('is_public', true) ->whereHasMorph('documentable', [Task::class], function ($query) { $query->where('client_id', $this->client()->id); }); } }