mardi 30 janvier 2018

Laravel whereBetween in two columns

i need to select products from DB by whereBetween. I have two double type columns in database, price and newprice. Price field is required and newprice is 0 by default. Admin writes newprice if there is any discounts on that product.

Now my problem is it that i need to select from DB with whereBetween by price if newprice == 0 and by newprice if newprice != 0. In a word filter by newprice if it is written, and by price if newprice is 0 by default.

Here is my code.

$from = (int)$request->priceFrom;
$to = (int)$request->priceTo;

$products = Product::whereIn('category_id', $childCategories)->orWhere('category_id', $category->id)->where('publish', 1)->orderBy('created_at', 'desc');
$products->whereBetween('price',array($from,$to))->orWhereBetween('newprice',array($from,$to));
$products = $products->paginate(15);

It doesn't works correctly ofc. Any ideas?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire