laravel条件数据库查询

时间:2014-07-04 21:13:04

标签: mysql sql laravel

我如何考虑我尝试和不工作的方式进行条件laravel查询,如果name不为空,我会尝试获取具有特定名称的产品

这个没有用!!

public function getProducts($page = null, $match=null, $price = null) {
    if ($page != null)
        DB::getPaginator()->setCurrentPage($page);
        $products = Product::join('product_specs', 'products.productid', '=', 'product_specs.product_id')
                ->join('feeds_categories', 'product_specs.category', '=', 'feeds_categories.id')
                ->where('feeds_categories.name', '=', 'Notebooks')
                ->where('products.status', '=', 1)
                ->where('products.price', '>', 250)
                ->orderBy('products.price', 'ASC')
                ->paginate(10);
   if($match !='') $products->where('productname','LIKE','%'.$match.'%');    
   return $products;
}

1 个答案:

答案 0 :(得分:2)

您没有将查询构建器重新分配回$products变量。试试这个:

if($match !='') $products = $products->where('productname','LIKE','%'.$match.'%');

事实上,您还需要在->paginate(10)之前移动它。

public function getProducts($page = null, $match=null, $price = null) {

        ...
        ->where('products.price', '>', 250); // Notice I added a semi-colon here.

    if($match !='') $products = $products->where('productname','LIKE','%'.$match.'%'); 

    $products = $products->orderBy('products.price', 'ASC')
        ->paginate(10);

    return $products;
}