Convert a JOIN to Laravel

1

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.email, 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
source

1 answer

2

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.email', 'c.telefono')
        ->join('agencia AS a', 'm.idmarca', '=', 'a.id_marca')
        ->join('contacto_agencia AS c', 'a.idagencia', '=', 'c.id_agencia')
        ->get();
  • 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

    var_dump($data);

    Demtro of the view, you do it

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