Laravel 5 Laravel 5.5:如何获得给定商店的最畅销商品?

Laravel 5 Laravel 5.5:如何获得给定商店的最畅销商品?,laravel-5,eloquent,laravel-5.5,Laravel 5,Eloquent,Laravel 5.5,我的桌子是这样的: 商店 [id] 库存 [id,shop\u id] 订单 [id,shop\u id] 订购商品 [订单id、库存id、数量] 型号: //Shop class Shop extends Model { public function inventories() { return $this->hasMany(Inventory::class); } public function orders() {

我的桌子是这样的:

商店
[id]

库存
[id,shop\u id]

订单
[id,shop\u id]

订购商品
[订单id、库存id、数量]

型号:

//Shop
class Shop extends Model
{
    public function inventories()
    {
        return $this->hasMany(Inventory::class);
    }

    public function orders()
    {
        return $this->hasMany(Order::class);
    }
}

//Inventory
class Inventory extends Model
{
    public function shop()
    {
        return $this->belongsTo(Shop::class);
    }

    public function orders()
    {
        return $this->belongsToMany(Order::class, 'order_items')
                    ->withPivot('quantity');
    }
}

//Order
class Order extends Model
{
    public function shop()
    {
        return $this->belongsTo(Shop::class);
    }

    public function inventories()
    {
        return $this->belongsToMany(Inventory::class, 'order_items')
                    ->withPivot('quantity');
    }
}
现在我想要一个给定商店的5个最畅销存货,最好的方法是什么


首先,我在Laravel 5.5上,通过查看您关于订单项目的表格,订单id和库存id肯定会在同一家商店中找到吗?我想是的,因为如果不是这样的话,你会有两个不同的商店有相同的最高订单。我不知道你为什么要这样做,但有点困惑,我不知道为什么,但我想试试这个:

select s.id,sum(oi.quantity) as total from munna.shops as s
join munna.inventories as iv on s.id=iv.shop_id
join munna.orders as o on iv.shop_id=o.shop_id
join munna.order_items as oi on o.id=oi.order_id
group by s.id
order by total desc limit 5
public function topOrders()
{
    $items = DB::table('shops')
                  ->join('orders', 'shops.id', '=', 'orders.shop_id')
                  ->join('inventories', 'shops.id', '=', 'inventories.shop_id')
                  ->join('order_items', 'orders.id', '=', 'order_items.order_id')
                  ->orderBy('quantity', 'desc')
                  ->take(5)
                  ->get();
    return $items;
}

我写的内容应该从所有3行中选择所有内容,如果您只想选择项目或任何您想选择的内容,您可以指定它,首先添加一个select子句,通过查看您在order\u item上的表,order\u id和存货\u id肯定会发送到同一家商店?我想是的,因为如果不是这样的话,你会有两个不同的商店有相同的最高订单。我不知道你为什么要这样做,但有点困惑,我不知道为什么,但我想试试这个:

public function topOrders()
{
    $items = DB::table('shops')
                  ->join('orders', 'shops.id', '=', 'orders.shop_id')
                  ->join('inventories', 'shops.id', '=', 'inventories.shop_id')
                  ->join('order_items', 'orders.id', '=', 'order_items.order_id')
                  ->orderBy('quantity', 'desc')
                  ->take(5)
                  ->get();
    return $items;
}

我写的内容应该从所有3行中选择所有内容,如果您只想选择项目或任何您想选择的内容,您可以指定它添加一个select子句

虽然这是我自己的问题,但我自己找到了解决方案,我想与社区共享该解决方案。我想用Eloquent解决它,因为我需要视图上的模型,不想再次查询模型

    Inventory::where('shop_id', \Auth::user()->shop_id)
                    ->select(
                        'inventories.*',
                        \DB::raw('SUM(order_items.quantity) as quantity')
                    )
                    ->join('order_items', 'inventories.id', 'order_items.inventory_id')
                    ->groupBy('inventory_id')
                    ->get();

我希望这将帮助有类似问题的人。谢谢

虽然这是我自己的问题,但我自己找到了解决方案,我想与社区分享解决方案。我想用Eloquent解决它,因为我需要视图上的模型,不想再次查询模型

    Inventory::where('shop_id', \Auth::user()->shop_id)
                    ->select(
                        'inventories.*',
                        \DB::raw('SUM(order_items.quantity) as quantity')
                    )
                    ->join('order_items', 'inventories.id', 'order_items.inventory_id')
                    ->groupBy('inventory_id')
                    ->get();

我希望这将帮助有类似问题的人。谢谢

你试过什么了吗?没有找到有用的东西!:(如果你有每个迁移的模型,你可以说类似于顺序模型使用商店模型,说
$this->hasMany('App\order','id');
等等。我想现在你明白了。你试过什么了吗?没有找到任何有用的东西!:(如果您有每个迁移的模型,您可以说类似于订单模型use shop model并说
$this->hasMany('App\order','id'))
等等。我想现在大家都明白了,我已经编辑了这个问题。是否可以使用Eloquent做一些事情?我不认为Eloquent可以处理这个复杂的查询。虽然如果是这样,那么成本会很高。使用DB::select()是否可以使用Eloquent做一些事情?我不认为Eloquent会处理这个复杂的查询。虽然如果是这样,那么它的成本会很高。使用DB::select()你将要发布一些东西,或者你是说你使用了我的asnwer?你将要发布一些东西,或者你是说你使用了我的asnwer?