Inner Join Laravel - Erro Where clause is ambiguous (23000)

Asked

Viewed 244 times

1

I am learning how to use Framwork Laravel and I came across the following error to perform an Join Ner (remembering that I already have a database with information so I am not used Migrations)

$colaborador = DB::table('colaboradores')
    ->join('beneficios','beneficios.colaborador_id','=','colaboradores.colaboradores_id')        
    ->where('colaborador_id',$request->colaborador_id)
    ->get();

And creates the error:

SQLSTATE[23000]: Integrity Constraint Violation: 1052 Column 'colaborador_id' in Where clause is ambiguous (23000)

but if I access the database and do the command:

select * from `colaboradores` inner join `beneficios` ON beneficios.colaborador_id = colaboradores.colaborador_id

Works perfectly.

1 answer

1


Your Select works because there is no clause Where. If you put a where colaborador_id = 1 in the query running in the database, you will get the same error.

This happens because both tables have colaborador_id then on your Where you can specify which colaborador_id you want to use, the colaboradores or of beneficios.

To do this simply insert the nomedatabela.campo in his Where

$colaborador = DB::table('colaboradores')
    ->join('beneficios','beneficios.colaborador_id','=','colaboradores.colaboradores_id')        
    ->where('beneficios.colaborador_id',$request->colaborador_id)
    ->get();
  • Thank you very much I managed to tidy up here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.