Buenas me gustaria pasar una consulta a Eloquent, pero no me funciona. creo que es por la subconsulta, pero vamos no lo se. Un saludo y gracias de antemano
Aqui dejo mi consulta de Eloquent la cual no esta bien hecha:
public function getDatas (){
$connection = $this->db->getDatabaseManager();
$subQuery = $this->db->connection('mirror')->table('bbdd.table5 dic')
->where('dic.company','=','XXXXX')
->whereRaw('date(dic.date_creation) >= date_add(curdate(), INTERVAL -5 DAY)')
->whereRaw('date(dic.date_creation) < curdate()');
$datas = $this->db->connection('mirror')->table('bbdd.table1 as din')
->join('bbdd.table2 as dcn','dcn.id_algo','=','din.id_algo')
->join('bbdd.table2 as dcn','dcn.id_otro','=','din.id_otro')
->join('bbdd.table3 as ds','ds.id_algo','=','din.id_algo')
->join('bbdd.table4 as dsn','and dsn.id','=','din.status')
->whereIn('din.id_algo',$subQuery)
->orderBy('din.dato3','ASC')
->orderBy('ds.dato5','ASC')
->select('din.dato1, din.dato2, din.dato3, dcn.dato4, ds.dato5, ds.dato6, dsn.dato7')
->get()
->map(function ($item, $key) {
return (array) $item;
})
->all();
return $datas;
}
Consulta Mysql
SELECT din.dato1, din.dato2, din.dato3, dcn.dato4, ds.dato5, ds.dato6, dsn.dato7
FROM
bbdd.table1 as din,
bbdd.table2 as dcn,
bbdd.table3 as ds,
bbdd.table4 as dsn
WHERE
din.id_algo in (
select dic.id_algo
FROM bbdd.table5 dic
where dic.company='XXXXX'
and date(dic.date_creation) >= date_add(curdate(), INTERVAL -5 DAY)
and date(dic.date_creation) < curdate()
)
and dcn.id_algo = din.id_algo
and dcn.id_otro = din.id_otro
and ds.id_algo = din.id_algo
and dsn.id = din.status
order by din.dato3 asc, ds.dato5 asc