【发布时间】:2019-02-26 19:50:08
【问题描述】:
我有一个嵌套的多级对象列表。 这是来自具有关系的雄辩查询。
一个地方有很多订单
一个订单有很多细节和一个客户
一个细节有一个产品和一个交货期
[
{
"place_id": 1,
"name": "A Building",
"address": "A Street, A Ward, A City",
"orders": [
{
"customer": {
"id": 1,
"name": "Peter",
"phone": "011111111"
},
"order_details": [
{
"period_id": 1,
"period": {
"id": 1,
"start_time": "08:00:00",
"end_time": "08:30:00"
},
"product_id": 1,
"product": {
"name": "A Cup of Tea"
}
}
]
},
{
"customer": {
"id": 2,
"name": "Mary",
"phone": "022222222222"
},
"order_details": [
{
"period_id": 1,
"period": {
"id": 1,
"start_time": "08:00:00",
"end_time": "08:30:00"
},
"product_id": 3,
"product": {
"name": "Glass of Milk Shake"
}
}
]
}
]
},
{
"place_id": 2,
"name": "B Building",
"address": "B Street, B Ward, B City",
"orders": [
{
"customer": {
"id": 3,
"name": "Catherine",
"phone": "0333333333"
},
"order_details": [
{
"period_id": 1,
"period": {
"id": 1,
"start_time": "08:00:00",
"end_time": "08:30:00"
},
"product_id": 2,
"product": {
"name": "A Cup of Coffee"
}
}
]
},
{
"customer": {
"id": 4,
"name": "Tom",
"phone": "04444444444444"
},
"order_details": [
{
"period_id": 2,
"period": {
"id": 1,
"start_time": "10:00:00",
"end_time": "10:30:00"
},
"product_id": 1,
"product": {
"name": "A Cup of Tea"
}
}
]
}
]
}
]
我需要按“句号”然后“地点”对这个列表进行分组
预期结果:
[
{
"id": 1,
"start_time": "08:00:00",
"end_time": "08:30:00",
"places": [
{
"place_id": 1,
"name": "A Building",
"address": "A Street, A Ward, A City",
"orders": [
{
"customer": {
"id": 1,
"name": "Peter",
"phone": "011111111"
},
"order_details": [
{
"period_id": 1,
"period": {
"id": 1,
"start_time": "08:00:00",
"end_time": "08:30:00"
},
"product_id": 1,
"product": {
"name": "A Cup of Tea"
}
}
]
},
{
"customer": {
"id": 2,
"name": "Mary",
"phone": "022222222222"
},
"order_details": [
{
"period_id": 1,
"period": {
"id": 1,
"start_time": "08:00:00",
"end_time": "08:30:00"
},
"product_id": 3,
"product": {
"name": "Glass of Milk Shake"
}
}
]
}
]
},
{
"place_id": 2,
"name": "B Building",
"address": "B Street, B Ward, B City",
"orders": [
{
"customer": {
"id": 3,
"name": "Catherine",
"phone": "0333333333"
},
"order_details": [
{
"period_id": 1,
"period": {
"id": 1,
"start_time": "08:00:00",
"end_time": "08:30:00"
},
"product_id": 2,
"product": {
"name": "A Cup of Coffee"
}
}
]
}
]
}
]
},
{
"id": 2,
"start_time": "10:00:00",
"end_time": "10:30:00",
"places": [
{
"place_id": 2,
"name": "B Building",
"address": "B Street, B Ward, B City",
"orders": [
{
"customer": {
"id": 4,
"name": "Tom",
"phone": "04444444444444"
},
"order_details": [
{
"period_id": 2,
"period": {
"id": 1,
"start_time": "10:00:00",
"end_time": "10:30:00"
},
"product_id": 1,
"product": {
"name": "A Cup of Tea"
}
}
]
}
]
}
]
}
]
这是我的详细代码:
class Place extends Model
{
public function orderDetails()
{
return $this->hasManyThrough(OrderDetail::class,
Order::class,'place_id','order_id','id','id');
}
}
class Period extends Model
{
public function details()
{
return $this->hasMany(OrderDetail::class, 'period_id', 'id');
}
}
class Order extends Model
{
public function place()
{
return $this->belongsTo(Place::class, 'place_id', 'id');
}
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
public function details()
{
return $this->hasMany(OrderDetail::class, 'order_id', 'id');
}
}
class OrderDetail extends Model
{
public function period()
{
return $this->belongsTo(Period::class, 'period_id', 'id');
}
public function product()
{
return $this->belongsTo(Product::class, 'product_id', 'id');
}
public function order()
{
return $this->belongsTo(Order::class, 'order_id', 'id');
}
}
Place::with(['orderDetails', 'orderDetails.product', 'orderDetails.period', 'orderDetails.order.user'])->get()->toArray();
我正在使用 laravel,所以最好使用 laravel 的集合助手来更快地解决这个问题。 请帮我 。谢谢
更新:
我使用了另一个查询和组 在 OrderDetail 模型中添加:
protected $appends = ['place_id'];
public function getPlaceIdAttribute()
{
return $this->order->place_id;
}
然后使用这个查询,我可以将期间分组而不是地点,但它只包含 id。
OrderDetail::with(['order','order.user','order.place','product','period'])->get()->groupBy(['period_id','place_id'])->toArray();
【问题讨论】:
-
如果您能提供我们迄今为止尝试过的代码,将会更快。
-
也许你可以通过 2 级的 belongsToManyThrough 关系来实现。您应该能够做到这一点
Period::with('places','places.orders','places.orders.customer','places.orders.order_details')->all();要实现第一部分,即获取期间的位置,您可以检查github.com/staudenmeir/eloquent-has-many-deep(注意它可能需要额外的数据库操作并且可能需要时间)
标签: laravel collections group-by nested multi-level