Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding a nested join 'in' Laravel Eloquent

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading