I am trying to get data from 2 tables in Laravel Query builder.
Here are the 2 tables I am using:
relation | administration |
---|---|
id | id |
administration_id | name |
invoice_mail | |
company | |
first_name | |
last_name |
So I want to fill a selectbox with the fields company
, first_name
, last_name
and administration.name
.
I am trying to get this with following query:
$Relations = Relation::where('in_profit','=',1)->where('error_profit','=',null);
$relation_options = [];
$Relations = $Relations
->join('administration', 'administration.id' , '=', 'relation.administration_id')
->select(DB::raw("id,mail, invoice_mail, administration.name, IFNULL(company, CONCAT(first_name,' ',last_name)) as fullname, CONCAT(first_name,' ',last_name) as name"))->get();
$relation_options = $Relations->pluck('fullname','id','administration.name' );
But i'm getting a 500 error, I think it has something to do with the join query.
This is the error:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous (SQL: select id,mail, invoice_mail, administration.name, IFNULL(company, CONCAT(first_name,' ',last_name)) as fullname, CONCAT(first_name,' ',last_name) as name from `relation` inner join `administration` on `administration`.`id` = `relation`.`administration_id` where `in_profit` = 1 and `error_profit` is null and `relation`.`deleted_at` is null)
Thanks already!