Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
Laravel9 cómo unir dos tablas si se cumple una de muchas condiciones

Tengo 2 tablas, quiero juntar todos los coletes que tengan el mismo id_factura que el id de facturas O si el id_factura = storno_id de facturas

 InvoicesID Coletes.INVOICE_ID 1 1 1 1

Pero también

 Invoices.STORNO_ID Coletes.INVOICE_ID 1 1 1 1

Mi consulta se ve así:

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

Pero solo devuelve los coletes con id_factura = $id, y no también los que tienen el mismo id_factura que facturas,storno_id

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

En las consultas SQL tradicionales, puede seleccionar entre varias tablas, como from invoices, coletes . Esto se puede hacer usando DB::raw() con el generador de consultas. Luego agregando la lógica de unión, como lógica where en su lugar.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!