query builder with inner join - laravel

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`)

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 to join multiple table in 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

Laravel Eloquent: how to use “where” with WithCount to select Models whose count is larger than a number

Suppose I have two tables: posts and tags. I want to eager load all the tags with posts whose post count is larger than 10. How would I do it?
I tried the following but not work:
Tag::withCount('posts')
->where('posts_count', '>' , 10)
->get();
It gives me the following error:
Column not found: 1054 Unknown column 'posts_count' in 'where clause' (SQL: select `tags`.*, (select count(*) from `posts` inner join `post_tag` on `posts`.`id` = `post_tag`.`post_id` where `tags`.`id` = `post_tag`.`tag_id`) as `posts_count` from `tags` where `posts_count` > 1 order by `posts_count` desc)
try this:
Tag::withCount('posts')->having('posts_count', '>' , 10)->get();
If you want to filter on aggregates, you need to use having.

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.

Laravel 4.2 Eloquent using lists() with a join query

I have a query that makes use of multiple joins:
public function scopePurchased($query, $userId)
{
return $query
->join('products','characters.id','=','products.productable_id')
->join('bundle_product','bundle_product.product_id','=','products.id')
->join('bundles','bundles.id','=','bundle_product.bundle_id')
->join('purchases','purchases.bundle_id','=','bundles.id')
->join('users','purchases.user_id','=','users.id')
->whereNull('purchases.deleted_at')
->where('purchases.refunded', false)
->where('products.productable_type', '=', get_class($this))
->where('users.id','=',$userId)
->groupBy('characters.id')
->orderBy('characters.title', 'ASC');
}
And I want to retrieve an array of ID's from this query to use in another scope so:
$query->purchased($userID)->lists('id')
My initial thought was to use lists('id') which complained about an ambiguous query on the ID.
Column 'id' in field list is ambiguous
(
SQL: select `id` from `characters`
inner join `products` on `characters`.`id` = `products`.`productable_id`
inner join `bundle_product` on `bundle_product`.`product_id` = `products`.`id`
inner join `bundles` on `bundles`.`id` = `bundle_product`.`bundle_id`
inner join `purchases` on `purchases`.`bundle_id` = `bundles`.`id`
inner join `users` on `purchases`.`user_id` = `users`.`id`
where `characters`.`deleted_at` is null
and `purchases`.`deleted_at` is null
and `purchases`.`refunded` = 0
and `products`.`productable_type` = Character and `users`.`id` = 1
group by `characters`.`id`
order by `characters`.`title` asc
)
Makes sense, fair enough so I changed the lists to
$query->purchased($userID)->lists('characters.id')
Thinking that naming the table and column should fix it but finding that the lists function drops the 'character.' part and so having the same error.
It appear that lists may not use a dot notation, bring me to my question... Can I escape the dot notation or is there another way to get the list of ID's as an array?
Many thanks
You can alias the column name before using lists:
$query->purchased($userID)->select('characters.id as _id')->lists('_id');
This will avoid any column name conflicts.

Resources