Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fredmoras's avatar

query builder

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(column ingredients.nom) for salade id 22.

sql query(work well):

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

$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.

0 likes
8 replies
topvillas's avatar

You're selecting from ingredients and doing an inner join on ingredients.

tykus's avatar

You are joining ingredients table to the query, but not providing an alias.

$Ingredients = DB::table('ingredients')
    ->select('ingredients.nom')
    ->join('salade_ingredient', 'salade_id', '=','22')
    // next you join ingredients to ingredients
    ->join('ingredients', 'ingredients.id', '=', 'salade_ingredient.ingredient_id')
    ->join('salades','salade.id','=','salade_ingredient.salade_id')
    ->get()
    ->distinct();

If this is intended, then alias the second ingredients :

// ...
    ->join('ingredients as ingredients2', 'ingredients2.id', '=', 'salade_ingredient.ingredient_id')
1 like
fredmoras's avatar

thanks, but with the new query i still have an error:

$Ingredients = DB::table('ingredients') ->select('ingredients.nom') ->join('salade_ingredient', 'salade_id', '=', '$Salade["id"]') ->join('ingredients as ingredients2', 'ingredients2.id', '=', 'salade_ingredient.ingredient_id') ->join('ingredients', 'ingredients.id', '=', 'salade_ingredient.ingredient_id') ->join('salades','salade.id','=','salade_ingredient.salade.id') ->select('ingredients.nom') ->get()->distinct();

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 as ingredients2 on ingredients2.id = salade_ingredient.ingredient_id inner join ingredients on ingredients.id = salade_ingredient.ingredient_id inner join salades on salade.id = salade_ingredient.salade.id)

lostdreamer_nl's avatar

Can I ask why you are not using Eloquent and it's relations?

2 models: salad & ingredient

class Salad extends Model
{
    public function ingredients()
    {
        return $this->belongsToMany(Ingredient::class);
    }
}


class Ingredient extends Model
{
    public function salads()
    {
        return $this->belongsToMany(Salad::class);
    }
}

$salad = Salad::find(22);
print_r($salad->ingredients->pluck('name')->toArray());

No more queries to think about (and be prone to errors)

1 like
topvillas's avatar

Look at the output. It's the same problem further on in the query.

fredmoras's avatar

thx lostdreamer_nl, the belongstomany relation was already made in the models, but i am new with laravel so i doesn't know this method.

but know i still have an error.

QueryException in Connection.php line 729: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'tuto.ingredient_salade' doesn't exist (SQL: select salades.*, ingredient_salade.salade_id as pivot_salade_id, ingredient_salade.ingredient_id as pivot_ingredient_id from salades inner join ingredient_salade on salades.id = ingredient_salade.ingredient_id where ingredient_salade.salade_id = 16)

fredmoras's avatar

the wright name of table pivot in not ingredient_salade but salade_ingredient

lostdreamer_nl's avatar

As fredmoras said: The pivot table is named wrong (normally you use the singular name of both tables, sort them alphabetically and use underscores to glue them together)

But: instead of changing your DB now, you can also tell Laravel that you're using a custom name for the pivot table.

return $this->belongsToMany(Salad::class, 'salade_ingredient');

(same goes for the Salad model's relationship to the Ingredient model)

I suggest reading up on Eloquent before going on with your project, it will save you a lot of time and headaches (what good is a toolbox if you're not using its tools) https://laravel.com/docs/5.4/eloquent-relationships#many-to-many

Please or to participate in this conversation.