在我的Laravel项目的控制器中,我想使用Product
模型获得两个顺序不同的列表,所以我在下面编写了代码:
$products = Product::where('status', 0);
$newProduct = $products->orderBy('created_at', 'desc')->limit(15)->get();
$hotProduct = $products->orderBy('click_counts', 'desc')->limit(15)->get();
我发现$newProduct
的排序正确,但$hotProduct
却没有。除非我这样更改代码:
$newProduct = Product::where('status', 0)->orderBy('created_at', 'desc')->limit(15)->get();
$hotProduct = Product::where('status', 0)->orderBy('click_counts', 'desc')->limit(15)->get();
我的问题是为什么我的第一个代码段错误并且无法给出预期的结果?
答案 0 :(得分:3)
尝试此查询:
$newProduct = Product::where('status', 0)->orderBy('created_at', 'desc')->limit(15)->get();
$hotProduct = Product::where('status', 0)->orderBy('click_counts', 'desc')->limit(15)->get();
答案 1 :(得分:0)
尝试一下
$products = Product::where('status', 0);
$newProduct = $products->orderBy('created_at', 'desc')->limit(15);
$hotProduct = $products->orderBy('click_counts', 'desc')->limit(15);
return view('foo.bar', ['newProduct', $newProduct->get(), 'hotProduct' => $hotProduct->get()]);