简体   繁体   中英

Laravel 5.4: in Eloquent using groupBy inside of relation whereHas

How can I use groupBy in whereHas in Eloquent

Here is my query

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 );
            } )
            ->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();

        //dd( $orders );
        //Order Status
        $orderStates = OrderState::listsTranslations( 'states' )->pluck( 'states', 'id' )->toArray();


        return view( 'supplierComponents.order_list', compact( 'orders', 'orderStates' ) );
    }

I need to group the list of the orders by order_id witch is exists in orderDetail

I have try to do so

->whereHas( 'orderDetail', function ( $query ) {
                $query->where( 'supplier_id', Auth::guard( 'supplier' )->user()->id )->groupBy('order_id');
            } ) 

Update

这是订单清单 and then

这是订单清单详细信息

Order list details has order_id witch I need the order_list to be groupBy it

dd($orders) results

11 => OrderList {#723 ▼
      #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:1 [▼
        "orderDetail" => Collection {#741 ▼
          #items: array:1 [▶]
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []

see this

#relations: array:1 [▼ "orderDetail" => Collection {#741 ▼ #items: array:1 [▶] }

if it's grouped it should get the related items grouped here but it's nit happening this way

but this didn't work, what should be the right way to do it?

whereHas wont give u group by, u have to get the relationship data again. use the following:

 $orders = ( new OrderList() )
            ->whereHas( 'orderDetail', function ( $query ) {
                $query->where( 'supplier_id', Auth::guard( 'supplier' )->user()->id );
            } )
            ->with(['orderDetail' => function ($query) {
                $query->where( 'supplier_id', Auth::guard( 'supplier' )->user()->id )->groupBy('order_id');
            }])
            ->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();
$orders = Order::join('orderDetail',function($q){
  $q->on('orderDetail.your_id','orders.id');
  $q->where('supplier_id', Auth::guard('supplier')->user()->id);
})
->select(['orders.user_id','orderDetail.order_id'])
->when($dateFrom, function ( $query ) use ( $dateFrom, $dateTo ) {
  $query->whereBetween('orders.created_at', [ $dateFrom, $dateTo ] );
})
->when($status, function ( $query ) use ( $status ) {
  $query->where( 'orders.order_state_id', $status );
})
->groupBy('orderDetail.order_id')
->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM