Home > Back-end >  Adding a nested join 'in' Laravel Eloquent
Adding a nested join 'in' Laravel Eloquent

Time:06-10

I want to join a table based on a couple of constraints, one of which is "where a value is IN an array".

I have this code as part of a complext query builder:

    $query->leftJoin('product_variable_values as val_alias, function ($join) use($rule) {
         $join->on('products.id', '=', 'val_alias.product_id')
              ->on('val_alias.product_variable_option_id', 'in', '(1,2,3)');
    });

Which outputs something like this:

left join `product_variable_values` as `val_ingredients` on `products`.`id` = `val_ingredients`.`product_id` and `val_ingredients`.`product_variable_option_id` = IN where `products`.`product_id` 

You will notice that there is an error in the mysql statement because I use IN as the operator which is not allowed.

Offending code:

->on('val_alias.product_variable_option_id', 'in', '(1,2,3)');:

Offending output:

val_ingredients`.`product_variable_option_id` = IN where `products`.`product_id

What is the correct way to do this?

CodePudding user response:

it same as :

$array = array(1,2,3);
$query->leftJoin('product_variable_values', 'products.id', '=', 'product_variable_values.product_id')
->whereIn('product_variable_values.product_variable_option_id', $array)
  • Related