Im trying to union 2 joins, but im getting weird error…
I have “Clientes” -> means clients, and i got all the clients that they got registered in orders and news.
In news table i got the cliente_id -> client_id , And in orders, i got the same, cliente_id -> client_id,
In news i got x clients, and in orders i got Y clients, they are 99% diferent usually,
I want to get all those clients in orders and in news.
So i did a MYSQL query that it works…
JavaScript
x
SELECT c.nombre, c.apellidos, c.id ,p.id FROM pedidos p
join clientes c on c.id = p.cliente_id
UNION
SELECT c.nombre, c.apellidos, c.id, n.id FROM noticias n
join clientes c on c.id = n.cliente_id
The problem is that in LAralvel o query builder it doesnt not work.
JavaScript
$firstSQL = Pedido::select('clientes.nombre','clientes.apellidos','clientes.id', 'pedidos.id')
->join('clientes', 'clientes.id', '=', 'pedidos.cliente_id')
->get();
$total = Noticia::select('clientes.nombre','clientes.apellidos','clientes.id', 'noticias.id')
->join('clientes', 'clientes.id', '=', 'noticias.cliente_id')
->union($firstSQL)
->get();
I got this error… BadMethodCallException Method IlluminateDatabaseEloquentCollection::getBindings does not exist.
Advertisement
Answer
remove get()
from first query
JavaScript
$firstSQL = Pedido::select('clientes.nombre','clientes.apellidos','clientes.id', 'pedidos.id')
->join('clientes', 'clientes.id', '=', 'pedidos.cliente_id');
$total = Noticia::select('clientes.nombre','clientes.apellidos','clientes.id', 'noticias.id')
->join('clientes', 'clientes.id', '=', 'noticias.cliente_id')
->union($firstSQL)
->get();