如果名称不为空,我将如何进行有条件的 laravel 查询,考虑到我尝试过但没有奏效的内容,我会尝试获取具有特定名称的产品
这个不行!!
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;
}
最佳答案
您没有将查询构建器重新分配回 $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;
}
关于mysql - laravel 条件数据库查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24580782/