ryanmortier
5 years ago
7949
9
General

How do you add a binding when using DB::raw in a join?

Posted 5 years ago by ryanmortier

The code:

$reserves = Commodity::select(
    'commodities.*',
    'bins.reserved'
)->leftJoin(
    DB::raw("
        (select
            `bins`.`commodity_id`,
            sum(`bins`.`capacity`) as `reserved`
        from `bins`
        where `bins`.`location_id` = :location_id
        group by `bins`.`commodity_id`) `bins`
    "), 'commodities.id', '=', 'bins.commodity_id'
)->get();

In the where clause, I need to substitute :location_id with a variable. How do I do this?

Please sign in or create an account to participate in this conversation.