我有array
个对象,其中某些对象具有cart_tables
null
,而另一些对象具有cart_tables
对象。所以我想要groupBy
cart_tables
cart_id
列之类的数据。
查询:
$timeTable = PivotTable::where('times_id', $timeId)
->with(['tables'])
->with(['orders_tables'])
->with(['cart_tables'])
->with([
'cart_tables' => function ($query) {
$query->whereNotNull('cart_id');
}
])
->whereHas('tables', function ($query) use ($restaurantId) {
$query->where('tables.rest_id', '=', $restaurantId);
})
->get()
->groupBy(function ($table, $key) {
if ($table['cart_tables']) {
return $table['cart_tables']['cart_id'];
} else {
return $table['id'];
}
})->values();
这里的问题是,如果false
中的条件groupBy
然后进入else
条件,而在else
条件中进入return key "$table['id']"
。 groupBy
条件的返回键相同时,则groupBy
结果为false
。
所以,我要对groupBy
数据做什么?
提前感谢!
使用如下所示的查询构建器:-
$timeTable = PivotTable::query();
$timeTable->where('times_id', $timeId)
->with(['tables'])
->with(['orders_tables'])
->with(['cart_tables'])
->with([
'cart_tables' => function ($query) {
$query->whereNotNull('cart_id');
}
])
->whereHas('tables', function ($query) use ($restaurantId) {
$query->where('tables.rest_id', '=', $restaurantId);
});
if(condition){
$timeTable->groupBy(..);
}else{
$timeTable->groupBy(..);
}
$result=$timeTable->values();