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

vincent15000's avatar

Equivalent between Eloquent and pure MySQL query

Hello,

I have an eloquent query which don't really work fine.

And I have the pure MySQL query which works as I want.

Even I have a distinct() method, for the product which have 2 units, only one unit is shown.

$products = DB::table('products')
    ->join('ingredients', 'ingredients.product_id', '=', 'products.id')
    ->join('units', 'units.id', '=', 'ingredients.unit_id')
    ->join('recipes', 'recipes.id', '=', 'ingredients.recipe_id')
    ->join('meals', 'meals.recipe_id', '=', 'recipes.id')
    ->groupByRaw('products.id, units.shortname')
    ->orderBy('products.name')
    ->distinct()
    ->selectRaw('products.id as id, sum(ingredients.quantity) as quantity, units.shortname as shortname')
    ->get()
    ->mapWithKeys(function ($item, $key) {
        return [
            $item->id => [
                'quantity' => $item->quantity,
                'shortname' => $item->shortname,
            ]
        ];
    });

Here it works fine.

select distinct
    products.id as id,
    sum(ingredients.quantity) as quantity,
    units.shortname as shortname
from
    products
    inner join ingredients on ingredients.product_id = products.id
    inner join units on units.id = ingredients.unit_id
    inner join recipes on recipes.id = ingredients.recipe_id
    inner join meals on meals.recipe_id = recipes.id
group by products.id, units.shortname
order by products.name asc

Here is an example of what I'd like to retrieve.

// What I get with the pure MySQL query
29, 5, kg
29, 350, g
29, 2, L

// What I get with the query builder
29, 2, L // => what I retrieve now with the query builder

The solution is perhaps to query with pure MySQL language. But how is it possible to run this query with Laravel ? And why doesn't the query builder give the same result ?

If you can help me ;) ... Thank you ;).

V

0 likes
10 replies
vincent15000's avatar

I've just seen that the problem seems not to be with the query but with my mapping after the query.

MichalOravec's avatar

@vincent15000 Yeah, the problem was your mapping after query.

What is the purpose of mapWithKeys?

Try to use mapToGroups instead.

1 like
vincent15000's avatar

@MichalOravec That's interesting ... What I need to do is to save the datas in a pivot table.

$cart = auth()->user()->cart()->sync($products);

I think that it won't be possible to do that in only one line. I will have to loop over each group and save each group separately with attach().

vincent15000's avatar

@MichalOravec I can possibly have some 20 ou 30 different products and I'd like to avoid to make 20 ou 30 queries to save the shopping list each time I add or remove a recipe.

MichalOravec's avatar
Level 75

@vincent15000 You can store only one product_id (with same value) per user in that table. So you can't store data like this

29, 5, kg
29, 350, g
29, 2, L

where product_id is 29.

1 like
vincent15000's avatar

@MichalOravec The product_id field is not set to unique in my table, but I understand the problem.

Do you suggest me to have a normal table ? Or what do you suggest me to save these data ?

This table will never have so much lines, always save, then deleted next week to be replaced by another list, ...

vincent15000's avatar

@MichalOravec

Initially I wanted to do like this, but the data are short-lived in this table.

It's a project to help to write a shopping-list to buy the food each week and each user has only one shopping-list (what I named cart). Once the food is bought, the shopping-list is empty (the datas are deleted).

That's why I thought about a simple pivot table. There is no reason to keep the datas.

But it's true that my idea is not the good relationship. I will change this.

Please or to participate in this conversation.