[英]Laravel 5.4: groupBy on for-loop not working as expected
首先是我的控制器,看這個控制器非常重要
public function index( Request $request ) {
$dateFrom = $request->get( 'dateFrom' );
$dateTo = $request->get( 'dateTo' );
$status = $request->get( 'orderState' );
$orders = ( new OrderList() )
->whereHas( 'orderDetail', function ( $query ) {
$query->where( 'supplier_id', Auth::guard( 'supplier' )->user()->id );
} )
->with( 'deliveryList' )
->when( $dateFrom, function ( $query ) use ( $dateFrom, $dateTo ) {
$query->whereBetween( 'created_at', [ $dateFrom, $dateTo ] );
} )
->when( $status, function ( $query ) use ( $status ) {
$query->where( 'order_state_id', $status )->get();
} )->get();
//Order Status
$orderStates = OrderState::listsTranslations( 'states' )->pluck( 'states', 'id' )->toArray();
return view( 'supplierComponents.order_list', compact( 'orders', 'orderStates' ) );
}
這也是OrderList
和OrderDetail
的模型
class OrderList extends Model {
protected $fillable = [
......
......
];
public function orderDetail() {
return $this->hasMany( OrderDetail::class, 'order_id' );
}
public function ....
....................
}
}
class OrderDetail extends Model {
protected $fillable = [
......
......
];
public function orderId() {
return $this->belongsTo( OrderList::class, 'order_id' );
}
public function productId() {
return $this->belongsTo( Product::class, 'product_id' );
}
}
現在我正在嘗試在刀片文件中進行分組
@foreach($orders as $order)
@foreach($order->orderDetail->groupBy('product_id') as $items)
<tr>
<td>{!! $items->sum('quantity') !!}</td>
<td>{!! $items->first()->product_name !!}</td>
<td>{!! $items->first()->product_id !!}</td>
</tr>
@endforeach
@endforeach
現在這個數組的結果像
-------------------------------------------
product-name | quantity | product-id |
-------------------------------------------
Coffey | 2 | 15 |
Coffey | 1 | 15 |
Coffey | 1 | 15 |
tea | 3 | 22 |
tea | 2 | 22 |
tea | 1 | 22 |
-------------------------------------------
我需要它像
-------------------------------------------
product-name | quantity | product-id |
-------------------------------------------
Coffey | 4 | 15 |
tea | 6 | 22 |
-------------------------------------------
更新資料
當調試我得到的命令時。
Collection {#708 ▼
#items: array:14 [▼
0 => OrderList {#717 ▼
#fillable: array:4 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:6 [▶]
#original: array:6 [▶]
#casts: []
#dates: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: array:5 [▼
"deliveryList" => null
"orderedUsers" => User {#756 ▶}
"orderedStates" => OrderState {#763 ▶}
"orderDetail" => Collection {#782 ▶}
"address" => CustomerAddressBook {#791 ▶}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
1 => OrderList {#718 ▶}
2 => OrderList {#719 ▶}
3 => OrderList {#720 ▶}
4 => OrderList {#721 ▶}
如何做到這一點
嘗試添加groupBy並在您的orderDetail關系中進行選擇 。
$orders = ( new OrderList() ) ->whereHas( 'orderDetail', function ( $query ) { $query->where( 'supplier_id', Auth::guard( 'supplier' )->user()->id ); $query->groupBy( 'product_id' ); $query->select(DB::raw( "SUM(quantity) as quantity_total" )); } )
並且quantity_total將保持總數量。
希望能有所幫助。
當您返回OrderList
類型而不是Collection
類型的對象時,您將無權訪問groupBy()
方法。
如果要對基礎查詢應用groupBy()
子句,可以通過訪問查詢構建器來實現,如下所示:
$order->orderDetail()->groupBy('product_id')
當您有權訪問Collection
( Eloquent
或Support
)時,可以按以下方式按列表中對象的屬性進行分組:
$order->groupBy('product_id');
但是,就您而言,我認為您實際上是想按相關對象的屬性分組。 您可以使用來執行此操作.
像這樣的符號:
$order->groupBy('orderDetail.product_id')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.