Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

87
Vistas
Laravel9 how to join two tables if one of many conditions is met

I have 2 tables, I want to gather all the coletes that have the same invoice_id as the invoices id OR if the invoice_id = invoices storno_id

InvoicesID      Coletes.INVOICE_ID 
1                  1
                   1
                   1 

But also

 Invoices.STORNO_ID      Coletes.INVOICE_ID
           1                        1
                                    1
                                    1

My query looks like this:

$data = DB::table('invoices')
->leftJoin('coletes','coletes.invoice_id','=','invoices.id')
->where('coletes.invoice_id','=',$id)
->orWhere('coletes.invoice_id','=','invoices.storno_id')
->get();

But it only returns the coletes with invoice_id = $id, and not also the ones with the same invoice_id as invoices,storno_id

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

In traditional SQL queries, you can select from multiple tables like so from invoices, coletes. This can be done by using DB::raw() with the query builder. Then adding the join logic, as where logic instead.

DB::table(DB::raw('invoices, coletes'))
    ->where('coletes.invoice_id', 'invoices.id')
    ->where(function($query) use ($id) {
        $query->where('coletes.invoice_id', $id)
            ->orWhere('coletes.invoice_id', 'invoices.storno_id')
    })->get();
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda