如何在Laravel雄辩中选择Select



我使用laravel 5.3

我的SQL查询是这样的:

SELECT * 
FROM (
    SELECT *
    FROM products 
    WHERE `status` = 1 AND `stock` > 0 AND category_id = 5
    ORDER BY updated_at DESC
    LIMIT 4
) AS product
GROUP BY store_id

我想将其更改为Laravel雄辩

但我仍然很困惑

我该怎么做?

在查询要复杂的情况下,您可以laravel RAW查询语法,例如:

$data = DB::select(DB::raw('your query here'));

它将在指定的表上发射您的原始查询,并返回结果集,如果有的话。

参考

如果您有产品模型,则可以运行

$products = Product::where('status', 1)
->where('stock', '>', 0)
->where('category_id', '=', 5)
->groupBy('store_id')
->orderBy('updated_at', 'desc')
->take(4)
->get();

我认为这应该给您带来相同的结果,因为您从派生的桌子上拉了所有东西。

最新更新