How to define an alias to columns with the same name in Laravel?

0

I have a question. how do I define the alias for columns with the same name ????. In particular I wish to consult my Database with records related to laravel in the following way

$proveedor =DB::table('proveedors')
            ->join('pais', 'proveedors.id_pais', '=', 'pais.id_pais')
            ->join('departamentos', 'proveedors.id_departamento', '=', 
              'departamentos.id_departamento')                ->select('nombre','representante','cargo','direccion','telefono','pais.nombre','departamentos.nombre', 'email','notas')
            ->where('id_proveedor', '=', $id)
            ->get();

But it returns an error because of the ambiguity in the column names.

    
asked by ALVARO ROBERTO BACARREZA ARZAB 08.05.2018 в 20:57
source

1 answer

2

You can use aliases in the following way:

$select =DB::table('tabla1 as t1')
->join('tabla2 as t2', 't1.id', '=','t2.id_tabla1')
->select('t1.uncampo','t2.otrocampo')
->where('t1.uncampo', '=', $id)
->get();

Greetings!

    
answered by 08.05.2018 в 21:38