Laravel 5.5: 如何获得给定商店的畅销商品?
Laravel 5.5: How to get top selling items of a given shop?
我的桌子是这样的:
商店
[id]
库存
[id, shop_id]
订单
[id, shop_id]
order_item
[order_id, inventory_id, quantity]
型号:
//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
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
首先,通过查看您在 order_item、、order_id 和 [=23= 上的表格]确定是同一家店吗?我想是的,因为如果不是,你将有 2 家不同的商店,最高订单相同。我不知道你为什么要这样做,但有点困惑,不知道为什么,但我会试试这个:
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;
}
我写的应该 select 所有 3 行的所有内容,如果你只想 select 项目或任何你想要的 select 你可以指定它添加一个 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();
我希望这会对遇到类似问题的人有所帮助。谢谢
我的桌子是这样的:
商店
[id]
库存
[id, shop_id]
订单
[id, shop_id]
order_item
[order_id, inventory_id, quantity]
型号:
//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
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
首先,通过查看您在 order_item、、order_id 和 [=23= 上的表格]确定是同一家店吗?我想是的,因为如果不是,你将有 2 家不同的商店,最高订单相同。我不知道你为什么要这样做,但有点困惑,不知道为什么,但我会试试这个:
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;
}
我写的应该 select 所有 3 行的所有内容,如果你只想 select 项目或任何你想要的 select 你可以指定它添加一个 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();
我希望这会对遇到类似问题的人有所帮助。谢谢