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

dmytroshved's avatar

How to query pivot table and related data in laravel query builder

Hey everyone, I need to get this data for recipe:

  • related users with name and photo (foreign key in recipes table: user_id)
  • related ingredients with name of ingredient name and unit name (pivot table ingredient_recipe)

users:

id, name, photo

(necessary columns: name & photo)

ingredient_recipe (pivot table):

id, recipe_id, ingredient_id, quantity, unit_id

ingredients:

id, name

units:

id, name

Code

For this moment I am getting user data related to recipe:

        $recipes = DB::table('recipes')
            ->join('users', 'user_id', '=', 'users.id')
            ->select(
                'recipes.*',
                'users.name AS user_name',
                'users.photo AS user_photo',
            )

But I am in trouble to build more complicated query with pivot tables and get data for ingredients and units

Would be grateful for some help

1 like
6 replies
vincent15000's avatar
Level 63

I have quite exactly the same database for my recipes application.

I just have a little difference : a products table with all products and an ingredients table for the ingredients with product_id.

The equivalent for your database is ingredients and ingredient_recipe.

Here is what I have done, but adapted to your database.

In the Recipe model.

public function user()
{
	return $this->belongsTo(User::class);
}

public function ingredients()
{
	return $this->hasMany(Ingredient::class, 'ingredient_recipe'));
}

In the Ingredient model.

protected $table = 'ingredient_recipe';

public function unit()
{
	return $this->belongsTo(Unit::class);
}

Then in your controller.

$recipes = Recipe::with('user', 'ingredients.unit')->get();

And in the view.

<ul>
	@foreach ($recipes as $recipe)
		<li>
			<div>{{ $recipe->user->name }}</div>

			<ul>
				@foreach ($recipe->ingredients as $ingredient)
					<li>{{ $ingredient->name }} ({{ $ingredient->quantity }} {{ $ingredient->unit->name }})</li>
				@endforeach
			</ul>
		</li>		
	@endforeach
</ul>
3 likes
hellolara's avatar

@vincent15000 This is the solution.

Basically for anyone coming in, having to explicitly write the relationship in the controller is not the right way to go about it and will result in similar code in many controllers as you write more controllers

2 likes
dmytroshved's avatar

@vincent15000 Yes, it is a very good solution! I have the same relations in my models

I wanna to share this approach for relations in pivot table

I am using this package:

composer require ajcastro/eager-load-pivot-relation

and that package allows me to use this syntax:

$recipes = Recipe::with('user', 'ingredients.pivot.unit')->get()...

the reason I am using this package: laravel can't load relations from pivot models, I was getting n+1 but this package solved the problem

1 like
GdS's avatar

IMHO recipe looks like a good candidate for a pivot model. Take a look at the docs: custom intermediate table models

I can't remember if there is any video about it in Laracasts, but a quick search may bring some useful results.

Alternatively you could also consider pivot methods

1 like

Please or to participate in this conversation.