Convert a JOIN to Laravel


I am in a small problem, I managed to make a query in MySQL to obtain data from three different tables, with these data I want to print them on my WEB page, I have researched and it turns out that I must use JSON and Ajax to be able to store my data and I can print them on my page with an @foreach, but even if I try not to convert my query to my Laravel driver so that it works, someone could help me explaining a bit about how the query is generated.

I leave my query

   select m.nombre_marca,a.nombre_producto , a.direccion_agencia, 
          c.nombre,, c.telefono FROM marca m 
   INNER JOIN agencia a ON m.idmarca = a.id_marca 
   INNER JOIN contacto_agencia c ON a.idagencia = c.id_agencia;

Thank you very much for your support!

asked by Bahamut4321 06.11.2018 в 23:37

1 answer


I tell you that you can transform your query in the following way

$data = \DB::table('marca AS m')
        ->select('m.nombre_marca', 'a.nombre_producto', 'a.direccion_agencia', 
                 'c.nombre', '', 'c.telefono')
        ->join('agencia AS a', 'm.idmarca', '=', 'a.id_marca')
        ->join('contacto_agencia AS c', 'a.idagencia', '=', 'c.id_agencia')
  • The joins() you achieve through the method ->join()
  • The select() I do before the joins indicating separated by commas and in quotes each of the desired columns
  • to check the data that arrives to you, it is enough that you do


    Demtro of the view, you do it

    @foreach($data as $d)
       {{ $d->columnaNombre }}
    answered by 07.11.2018 / 00:10