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

vincej's avatar
Level 15

Need Help On Translating a Complex Query into Laravel

Because I am old school, I have a habit of first building my queries in SQL, and then translating them into Query Builder. But this one beats me. The SQL works fine. But can someone offer some guidance on getting into Laravel? What is messing me up is that there is a subquery and two joins plus a sum().

I noticed that Laravel 6 now offers subqueries, but they are super simple and have not helped me. However, I have now at least upgraded to v6.8 !

I would be super happy if I could just call the raw SQL somehow, but that is not obvious either as when I try I am not getting a connection to the DB. So, if I could figure that out, that would be my first choice. Anyway, here is the SQL. Many Thanks ! :

select orders.product_id, products.product_name, sum(orders.quantity) as orders, stock
from orders
    left join
    (select inventory.inv_product_id,
    sum(inventory.stock_quantity) as stock
    from inventory
    group by inventory.inv_product_id ) i
    on inv_product_id = orders.product_id
    left join products on orders.product_id = products.product_id
    group by orders.product_id


0 likes
4 replies
jlrdw's avatar

Can use as is see https://laracasts.com/discuss/channels/laravel/sql-native-to-query-builder

Example of eloquent query

This is just an example:

$quy = Powner::query()->leftJoin('dc_pets', 'dc_powners.ownerid', '=', 'dc_pets.ownerid')
                ->select('dc_powners.ownerid', 'dc_powners.oname')
                ->selectRaw('count(dc_pets.petid) as countOfPets')
                ->groupby('dc_powners.ownerid')
                ->orderby('dc_powners.oname')
                ->get();

Results basically give:

ownerid, oname, countOfPets

Like:

5|Bob|3
4|Greg|9
2|Rob|1
// more

@vincej remember these types of queries can take a little trial and error.

vincej's avatar
Level 15

Ok - in the end it was easy, all I had to do was this:

public function inventory_by_product() {
        $result = DB::select('select orders.product_id, products.product_name, sum(orders.quantity) as orders, stock
        from orders
        left join
        (select inventory.inv_product_id,
        sum(inventory.stock_quantity) as stock
        from inventory
        group by inventory.inv_product_id ) i
        on inv_product_id = orders.product_id
        left join products on orders.product_id = products.product_id
        group by orders.product_id');
        return $result;
              }


vincej's avatar
Level 15

@jlrdw Thanks a bunch !! Our messages crossed paths!!

Anyway, now that I have found out how to use raw queries in Laravel I am as happy as pig in poo. I will still carry on using query builder for easy stuff, and especially so when sanitized inputs are used.

Cheers ! as always !

jlrdw's avatar

@vincej also see the link above which shows an example of bindings.

Please or to participate in this conversation.