juanpscotto
juanpscotto

Reputation: 1050

How to do this JOIN query in Laravel?

I'm trying to do this join in laravel query builder but it is throwing and error. I'm using and Mysql DB. This is my sql:

select c.*, d.* from notas_cabecera c
join notas_detalle d on (c.codigo_nota = d.codigo_nota)
where c.codigo_nota in (select r.codigo_nota from reportes r); 

And this is my laravel query:

$lista_reportes = DB::table('notas_cabecera')
                              ->join('notas_detalle', 'notas_cabecera.codigo_nota', '=', 'notas_detalle.codigo_nota')
                              ->whereIn('notas_cabecera.codigo_nota', function($query)
                              {
                                    $query->select(
                                              DB::table('reportes')->select('reportes.codigo_nota')
                                            );
                              })
                              ->get();

What I'm doing wrong? please help.

Upvotes: 0

Views: 63

Answers (1)

Tim
Tim

Reputation: 285

$lista_reportes = DB::table('notas_cabecera')
                              ->join('notas_detalle', 'notas_cabecera.codigo_nota', '=', 'notas_detalle.codigo_nota')
                              ->whereIn('notas_cabecera.codigo_nota', function($query)
                              {
                                    $query
                                        ->select('reportes.codigo_nota')
                                        ->from('reportes');
                              })
                              ->get();

Upvotes: 1

Related Questions