title: Laravel 5.4: in Eloquent using groupBy inside of relation whereHas
body: <p>How can I use <code>groupBy</code> in <code>whereHas</code> in Eloquent</p>
  
  <p>Here is my query</p>
  
  <pre><code>public function index( Request $request ) {
          $dateFrom = $request-&gt;get( 'dateFrom' );
          $dateTo   = $request-&gt;get( 'dateTo' );
          $status   = $request-&gt;get( 'orderState' );
  
          $orders = ( new OrderList() )
              -&gt;whereHas( 'orderDetail', function ( $query ) {
                  $query-&gt;where( 'supplier_id', Auth::guard( 'supplier' )-&gt;user()-&gt;id );
              } )
              -&gt;when( $dateFrom, function ( $query ) use ( $dateFrom, $dateTo ) {
                  $query-&gt;whereBetween( 'created_at', [ $dateFrom, $dateTo ] );
              } )
              -&gt;when( $status, function ( $query ) use ( $status ) {
                  $query-&gt;where( 'order_state_id', $status )-&gt;get();
              } )
              -&gt;get();
  
          //dd( $orders );
          //Order Status
          $orderStates = OrderState::listsTranslations( 'states' )-&gt;pluck( 'states', 'id' )-&gt;toArray();
  
  
          return view( 'supplierComponents.order_list', compact( 'orders', 'orderStates' ) );
      }
  </code></pre>
  
  <p>I need to group the list of the orders by <code>order_id</code> witch is exists in <code>orderDetail</code> </p>
  
  <p>I have try to do so</p>
  
  <pre><code>-&gt;whereHas( 'orderDetail', function ( $query ) {
                  $query-&gt;where( 'supplier_id', Auth::guard( 'supplier' )-&gt;user()-&gt;id )-&gt;groupBy('order_id');
              } ) 
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p><a href="https://i.stack.imgur.com/OgOlQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OgOlQ.png" alt="Here is the Order List"></a>
  and then </p>
  
  <p><a href="https://i.stack.imgur.com/mfLH4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mfLH4.png" alt="Here is the Order List Details"></a></p>
  
  <p>Order list details has <code>order_id</code> witch I need the order_list to be <code>groupBy</code> it</p>
  
  <p><code>dd($orders) results</code></p>
  
  <pre><code>11 =&gt; 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" =&gt; Collection {#741 ▼
            #items: array:1 [▶]
          }
        ]
        #touches: []
        +timestamps: true
        #hidden: []
        #visible: []
  </code></pre>
  
  <p>see this</p>
  
  <p><code>#relations: array:1 [▼
              "orderDetail" =&gt; Collection {#741 ▼
                #items: array:1 [▶]
              }</code></p>
  
  <p>if it's grouped it should get the related items grouped here but it's nit happening this way</p>
  
  <p>but this didn't work, what should be the right way to do it?</p>