Convert mysql to laravel 5 eloquent (left join with select)

492 views Asked by At

I am stuck at rewriting mysql query to laravel eloquent.

LEFT JOIN `products_options` ON `products_options`.`id` = (
        SELECT products_options.id FROM products_options WHERE 1
        AND products_options.product_id = products.id 
        AND products_options.color = $color
    )

Any help?

1

There are 1 answers

0
Chris Townsend On BEST ANSWER

To replicate that query use the following

$products = DB::table ('product_options')
    ->leftJoin ('products', 'product_options.product_id', '=', 'products.id')
    ->where('product_options.product_id', '=', 'products.id')
    ->where('product_options.color', '=', $color)->get();

Your query looks a little off but this is it replicated using the eloquent query builder. Don't forget to reference DB in your controller or model