Hello everyone, I would like to know if there is a better way to do this type of query, I have achieved it through a sub query but I want to know if there is a better or more efficient way with eloquent, the case is that in a one-to-many relationship what I want the query to return is, passing a specific element through id, it returns all the elements related to it. That is, between a job and a client I want him to return all the works of a single client. Thanks!!
My code:
public function render()
{
$works = Work::where('client_id',$this->client->id)
->where(function ($query) {
$query->where('id','like','%' . $this->search . '%')
->orWhere('code_work','like','%' . $this->search . '%')
->orWhere('date','like','%' . $this->search . '%')
->orWhere('description','like','%' . $this->search . '%');
})->orderBy($this->sort, $this->direction)->paginate(10);
return view('livewire.admin.clients.works',['works' => $works]);
}
I tried this but not work:
public function render()
{
$works = Work::with('client')->find($this->client->id)->where('id','like','%' . $this->search . '%')
->orWhere('code_work','like','%' . $this->search . '%')
->orWhere('date','like','%' . $this->search . '%')
->orWhere('description','like','%' . $this->search . '%')
->orderBy($this->sort, $this->direction)->paginate(10);
return view('livewire.admin.clients.works',['works' => $works]);
}