我在操纵雄辩的物体时遇到了问题。
基本上我想对数据进行排序(db results),并按特定属性对其进行分组。
问题是,我不知道如何使用对象来完成它。(我实际上通过转换结果来实现它
public static function groupOrderProductsByOrderNumber($orderProducts)
{
$results = array();
if(isset($orderProducts) && $orderProducts->count())
{
foreach ($orderProducts as $key => $value) {
$results[$value->order_id]['products'][] = $value;
}
}
return $results;
}
Array
(
[2] => Array
(
[products] => Array
(
[0] => OrderProduct Object
(
[table:protected] => order_product
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 3
[order_id] => 2
[product_id] => 23
[store_id] => 4
[name] => Nike Shoes
[model] => HD 2014
[quantity] => 1
[price] => 8000
[total] => 80000
[tax] => 0.00
[created_at] => 0000-00-00 00:00:00
[updated_at] => 0000-00-00 00:00:00
[commission] => 3
)
[original:protected] => Array
(
[id] => 3
[order_id] => 2
[product_id] => 23
[store_id] => 4
[name] => Nike Shoes
[model] => HD 2014
[quantity] => 1
[price] => 8000
[total] => 80000
[tax] => 0.00
[created_at] => 0000-00-00 00:00:00
[updated_at] => 0000-00-00 00:00:00
[commission] => 3
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
[softDelete:protected] =>
)
)
)
[4] => Array
(
[products] => Array
(
[0] => OrderProduct Object
(
[table:protected] => order_product
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 1
[order_id] => 4
[product_id] => 1
[store_id] => 4
[name] => Nike Socks
[model] => Hyperelite
[quantity] => 1
[price] => 845
[total] => 845
[tax] => 0.00
[created_at] => 0000-00-00 00:00:00
[updated_at] => 0000-00-00 00:00:00
[commission] => 0
)
[original:protected] => Array
(
[id] => 1
[order_id] => 4
[product_id] => 1
[store_id] => 4
[name] => Nike Socks
[model] => Hyperelite
[quantity] => 1
[price] => 845
[total] => 845
[tax] => 0.00
[created_at] => 0000-00-00 00:00:00
[updated_at] => 0000-00-00 00:00:00
[commission] => 0
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
[softDelete:protected] =>
)
[1] => OrderProduct Object
(
[table:protected] => order_product
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 2
[order_id] => 4
[product_id] => 1
[store_id] => 4
[name] => Nike Socks
[model] => Hyperelite
[quantity] => 1
[price] => 845
[total] => 845
[tax] => 2.00
[created_at] => 0000-00-00 00:00:00
[updated_at] => 0000-00-00 00:00:00
[commission] => 0
)
[original:protected] => Array
(
[id] => 2
[order_id] => 4
[product_id] => 1
[store_id] => 4
[name] => Nike Socks
[model] => Hyperelite
[quantity] => 1
[price] => 845
[total] => 845
[tax] => 2.00
[created_at] => 0000-00-00 00:00:00
[updated_at] => 0000-00-00 00:00:00
[commission] => 0
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
[softDelete:protected] =>
)
)
)
)
基本上,我希望使用上面的代码获得相同的结果,但使用Eloquent Object(而不是数组)。这是为了让我能够使用Eloquent函数,如(分页,链接等)。
答案 0 :(得分:0)
如果您使用laravel方式获取结果,您是否尝试过groupBy方法?
$orderProducts = OrderProduct::where('orderId')
->groupBy('id')
->get()
当然,如果您想查看所有订单产品并省略`get()
,您可以使用all()
在你的帖子中不清楚你设置了什么关系,所以这可能不是你情况的完美答案。