mercredi 4 octobre 2017

How can I add condition in db raw laravel?

My query db raw laravel like this :

public function getTopProduct($price = null) {
    $products =  DB::select(DB::raw('SELECT * 
                    FROM (
                        SELECT a.*, b.name AS store_name, b.address
                        FROM products a
                        JOIN stores b ON b.id = a.store_id
                        WHERE a.status = 1                                                
                    ) AS product
                    GROUP BY store_id')
        );
    return $products;
}

I want to add condition

If price is not null, it will add condition price on the where

For example, price = 1000, then the query on the where like this :

WHERE a.status = 1 AND a.price < 1000

If price = null, the condition AND a.price < 1000 not executed

How can I do it?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire