简体   繁体   English

如何在laravel上进行雄辩的选择?

[英]How can I make select in select on laravel eloquent?

I use laravel 5.3 我使用laravel 5.3

My sql query is like this : 我的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

I want to change it to be laravel eloquent 我想将其更改为Laravel雄辩

But I'm still confused 但是我还是很困惑

How can I do it? 我该怎么做?

In cases when your query is to complex you can laravel RAW query syntax like: 在查询复杂的情况下,您可以使用laravel RAW查询语法,例如:

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

It will fire your raw query on the specified table and returns the result set, if any. 它将在指定的表上触发原始查询,并返回结果集(如果有)。

Reference 参考

If you have Product model, you can run 如果您具有产品模型,则可以运行

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

I think this should give you the same result since you pull everything from your derived table. 我认为这应该给您相同的结果,因为您从派生表中提取了所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM