How to join multiple table in Laravel? - laravel

I have 3 table
1- planes = name, description
2- presidents = id , p_name
3- plane_president = plane_id , president_id
how to join planes with presidents and plane_president?
$planes = Plane::join('plane_president', 'planes.id', '=', 'plane_president.plane_id')
->join('presidents', 'presidents.id', '=', 'plane_president.president_id');
error:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in order clause is ambiguous (SQL: select * from `planes` inner join `plane_president` on `planes`.`id` = `plane_president`.`plane_id` inner join `presidents` on `presidents`.`id` = `plane_president`.`president_id` order by `id` asc limit 10 offset 0)

plane_president is a pivot table for president and planes, you can declare the relationship at the Plane.php model:
public function presidents()
{
return $this->belongsToMany(
President::class,
'plane_president',
'plane_id',
'president_id');
}
and use it like:
plane->presidents;
when you want to retrieve the presidents for a particular plane in a controller.

You have 2 columns named id as the error says it is ambiguous.
order by id asc limit 10 offset 0 here you aren't specifying by which column to order planes.id or presidents.id

Related

Laravel ambiguous key in 'where' clause

I have a customer class and a pet class which has a many to many relationship. There is a pivot table customer_pet. Both the customers table and the pets table have a team_id.
When I write the following code:
$results = auth()->user()->team->customers();
$results->leftJoin('customer_pet', 'customer_pet.customer_id', '=', 'customers.id');
$results->leftJoin('pets', 'pets.id', '=', 'customer_pet.pet_id');
return $results->get()->load('pets');
I get this error:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'team_id' in where clause is ambiguous (SQL: select * from `customers` left join `customer_pet` on `customer_pet`.`customer_id` = `customers`.`id` left join `pets` on `pets`.`id` = `customer_pet`.`pet_id` where `customers`.`team_id` = 2 and `customers`.`team_id` is not null and `customers`.`deleted_at` is null and `team_id` = 2
I tried making the relation in the Customer model look like this:
public function team()
{
return $this->belongsTo(Team::class, 'customers.team_id', 'teams.id');
}
but I get the same result. I am missing something obvious but I am not sure what?
Any help would be appreciated. Thanks.
select * from `customers` left join `customer_pet` on `customer_pet`.`customer_id` = `customers`.`id` left join `pets` on `pets`.`id` = `customer_pet`.`pet_id` where `customers`.`team_id` = 2 and `customers`.`team_id` is not null and `customers`.`deleted_at` is null and `team_id` = 2
The query in the exception shows that there is a team_id = 2 at the end. It seems that you're adding a $query->where('team_id', 2) without qualification somewhere in your code.
You can use $query->qualifyColumn($column) to prepend the table name automatically.

How can i convert this raw query to eloquent scope?

SELECT *
FROM employment_informations t1
WHERE NOT EXISTS (
SELECT 1 FROM employment_informations t2
WHERE t1.employee_id = t2.employee_id
AND t1.field_name = t2.field_name
AND t2.created_at > t1.created_at
)
This is what I have so far.
$builder->whereNotExists(function ($builder) {
$builder->select(\DB::raw(1))
->from('employment_informations t2')
->whereRaw('employment_informations.employee_id = t2.employee_id
AND employment_informations.field_name = t2.field_name
AND t2.created_at > employment_informations.created_at
');
});
I don't know how to alias table in eloquent scope.
here's the error:
[09:15:38] LOG.error: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'hris.employment_informations t2' doesn't exist (SQL: select count(*) as aggregate from `employment_informations` where not exists (select 1 from `employment_informations t2` where employee_id = t2.employee_id
AND field_name = t2.field_name
AND t2.created_at > created_at)) {"userId":1,"exception":{"errorInfo":["42S02",1146,"Table 'hris.employment_informations t2' doesn't exist"]}}
doesn't work using as either:
->from('employment_informations as t2')
If possible I want to alias the table with t1 and the subquery table to t2, but I don't know how to do it.
Laravel supports aliases on tables and columns with AS. Try
$query = DB::table('tablename AS t')->select('t.id AS uid')->get();

how to join 3 tables using laravel eloquent?

my table
Code:
function viewPDF()
{
$reports = Report::join('president_report', 'reports.id', '=', 'president_report.report_id')
->join('president_report', 'presidents.id', '=', 'president_report.president_id')->
select('reports.*')->where('president_report.report_id')
->filter()->latest()->get();
$pdf = PDF::loadView('reports.test1', ['reports' => $reports]);
return $pdf->stream('reports.pdf');
}
Error:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique
table/alias: 'president_report' (SQL: select * from reports inner
join president_report on reports.id =
president_report.report_id inner join president_report on
presidents.id = president_report.president_id where
president_report.report_id is null order by created_at desc)
Use separate aliases for the two joins with the president_reports table:
function viewPDF() {
$reports = Report::join('president_report pr1', 'reports.id', '=', 'pr1.report_id')
->join('president_report pr2', 'presidents.id', '=', 'pr2.president_id')->
select('reports.*')->where('pr1.report_id')
->filter()->latest()->get();
$pdf = PDF::loadView('reports.test1', ['reports' => $reports]);
return $pdf->stream('reports.pdf');
}
Note that the second join looks suspicious to me, because I don't see where the presidents table is getting included in the join query. But the general solution to use problem is to alias president_report differently for the two joins.

query builder with inner join

I 've got a problem with the laravel query builder.
I don't understand what 's wrong.
I have 3 tables
salades
ingredients
salade_ingredient (pivot for n:n relation)
I would like to list the name of ingredients i.e. column ingredients.nom for salade id 22.
sql query(work):
select distinct ingredients.nom
from ingredients, salade_ingredient,salades
where salade_ingredient.salade_id = 22
and ingredients.id = salade_ingredient.ingredient_id
laravel query (error):
$Ingredients = DB::table('ingredients')
->select('ingredients.nom')
->join('salade_ingredient', 'salade_id', '=','22')
->join('ingredients', 'ingredients.id', '=', 'salade_ingredient.ingredient_id')
->join('salades','salade.id','=','salade_ingredient.salade_id')
->get()->distinct();
can you help me please? i am new with laravel.
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'ingredients' (SQL: select `ingredients`.`nom` from `ingredients` inner join `salade_ingredient` on `salade_id` = `$Salade["id"]` inner join `ingredients` on `ingredients`.`id` = `salade_ingredient`.`ingredient_id` inner join `salades` on `salade`.`id` = `salade_ingredient`.`salade`.`id`)

Laravel mysqli connect 2 columns and change column name

I have 2 tables that I want to connect
table1:
id
name
table2_id
table_2:
id
name
table1 is connected with table2
When I try to select table1 and the connected row from table2 it works, but because the row names id and name are the same, it overwrites the values of table1.
How can I connect table1 with table2 and get a table1 with this rows in laravel:
id
name
table2_id
table2_name
Review the laravel documentation regarding relationships - If you set up your models as outlined here https://laravel.com/docs/5.3/eloquent-relationships you will be able to call the table2 like:
$result = Table1::find(1)->table2;
However to get the result you want you can use the ->select( function
DB::table('users')->select('name', 'email as user_email')->get();
https://laravel.com/docs/5.2/queries#selects
Something like this
public function scopetable1($query,$id){
return $query = DB::table('table1')
->select('table1.id as id','table1.name as name','table2.id as table2_id', 'table2.name as table2_name' )
->where('table1.id', '=', $id)
->leftJoin('table2', 'table1.table2_id', '=', 'table2.id')
->get();
}

Resources