I need to do a query using Eloquent on a couple of tables that are 1-to-many related and I'm not able to figure it out. Here is the code I am using:
$datos = Vigelectrodetenida::query()
->with('relUsr')
->where('cam1b', 'LIKE', '%' . $this->busquedas . '%')
->orWhere('cam1c','LIKE', '%' . $this->busquedas . '%')
->orderBy($this->campOrden, $this->direcOrden)
->paginate(20);
I use this code to generate a table that has a lookup input using livewire. For now, the queries I make on the cam1b or cam1c field work fine, but when I want to put another orWhere and have it act on the name field that is in the users table (relUsr) it stops working, it tells me that the name field. Next I put the code that I am using and the error that is being generated.
$datos = Vigelectrodetenida::query()
->with('relUsr')
->where('cam1b', 'LIKE', '%' . $this->busquedas . '%')
->orWhere('cam1c','LIKE', '%' . $this->busquedas . '%')
->orWhere('name','LIKE', '%' . $this->busquedas . '%')
->orderBy($this->campOrden, $this->direcOrden)
->paginate(20);
Well, I hope I have been able to convey my doubt correctly and that you can help me.
I want to thank @HeytalePazguato for his valuable help
Good day,
One way to do it is with
select()
andjoin()
The general way to build it is like this
Where:
Using the data you put in your question and comments (As you did not add your migrations or your models you may have to adapt them)
After that you can already add your
where()
, and all the fields that are from the tableusers_dnpc
will have to be declared through the table name, for example:Note: If there is any column that has the same name in both tables (For example, in case you use
created_at
andupdated_at
) you will have to declare them through the table name, that is,users_dnpc.created_at
andsi05_vigelec_dtosdetenida.created_at