Select distinct () with Join in Laravel 5.5

0

I've been working on an sql query that I want to replicate in Laravel 5.5 with eloquent, but I can not get it to work. I explain, this is my query in SQL:

SELECT
empresas.companyname_cny,
empresas.ruc_cny,
municipios.municipality_mty,
ciudads.city_cty,
negocios.giro_detalle,
empresas.address_cny,
empresas.phone_cny,
empresas.email_cny,
empresas.id,
tpoders.id
FROM
empresas
INNER JOIN municipios ON empresas.municipalityid_cny = municipios.id
INNER JOIN ciudads ON empresas.cityid_cny = ciudads.id
INNER JOIN negocios ON empresas.gnegocio_id = negocios.id
INNER JOIN tpoders ON tpoders.empresa_id = empresas.id
GROUP BY empresas.companyname_cny

and my result is how I expect it:

But when doing it in laravel 5.5:

 $dem = DB::table('empresas')
      ->select(
        'empresas.companyname_cny',
        'tpoders.id',
        'empresas.id',
        'empresas.companyname_cny',
        'negocios.giro_detalle',
        'empresas.phone_cny',
        'empresas.email_cny',
        'ciudads.city_cty'
      )
      ->join('ciudads', 'empresas.cityid_cny','=','ciudads.id')
      ->join('negocios','empresas.gnegocio_id','=','negocios.id')
      ->join('tpoders','tpoders.empresa_id','=','empresas.id')
      ->distinct()->get(['empresas.companyname_cny']);

The result he gives me is the following:

The Company column repeats. I want to avoid that. Sure it's something simple, but I spent the day looking for a solution and I could not find it. Thank you very much for your time.

Greetings

    
asked by Luis Medina 28.04.2018 в 04:38
source

1 answer

0
$dem = DB::table('empresas')
      ->select(
        'empresas.companyname_cny',
        'tpoders.id',
        'empresas.id',
        'empresas.companyname_cny',
        'negocios.giro_detalle',
        'empresas.phone_cny',
        'empresas.email_cny',
        'ciudads.city_cty'
      )
      ->join('ciudads', 'empresas.cityid_cny','=','ciudads.id')
      ->join('negocios','empresas.gnegocio_id','=','negocios.id')
      ->join('tpoders','tpoders.empresa_id','=','empresas.id')
      ->distinct()->get();

That should work but check that all the columns are repeated.

  

The distinct method allows you to force the query to return   different results

    
answered by 02.05.2018 / 16:27
source