title: Grouping data inside foreach in blade in laravel
body: <p>I am trying to group a product name in may table. I found a suggestion to put a ->groupBy in my controller but when I am doing it my relation to other model is broken. what is the bets approach to achieve my what I want?</p>
  
  <p>here's my code in my view blade.</p>
  
  <pre><code>&lt;table class="table table-striped m-b-none" data-ride="datatables" id="table"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th width=""&gt;Product Code&lt;/th&gt;
              &lt;th width=""&gt;Product Name&lt;/th&gt;
              &lt;th width=""&gt;In&lt;/th&gt;
              &lt;th width=""&gt;Out&lt;/th&gt;
              &lt;th width=""&gt;Total Stocks&lt;/th&gt;
              &lt;th width=""&gt;Note&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          @foreach( $warehouse1stocks as $warehouse1stock )
          @php
          $totalStocks = $warehouse1stock-&gt;stock_in_qty - $warehouse1stock-&gt;stock_out_qty;
          $stockWarning = $warehouse1stock-&gt;product-&gt;wh1_limit_warning;
          @endphp
          &lt;tr&gt;
              &lt;td&gt;{{$warehouse1stock-&gt;orderItem-&gt;product_code}}&lt;/td&gt;
              &lt;td&gt;{{$warehouse1stock-&gt;orderItem-&gt;product_name}}&lt;/td&gt;
              &lt;td&gt;{{$warehouse1stock-&gt;stock_in_qty}}&lt;/td&gt;
              &lt;td&gt;{{$warehouse1stock-&gt;stock_out_qty}}&lt;/td&gt;
              @if($totalStocks &lt;= $stockWarning||$totalStocks==$stockWarning) &lt;td&gt;{{$totalStocks}} &lt;i data-toggle="tooltip" data-placement="top" title="below stock limit, {{$stockWarning}}" class="fas fa-exclamation-circle text-danger"&gt;&lt;/i&gt;&lt;/td&gt;
                  @else
                  &lt;td&gt;{{$totalStocks}}&lt;/td&gt;
                  @endif
                  &lt;td&gt;{{$warehouse1stock-&gt;delivery_note}}&lt;/td&gt;
          &lt;/tr&gt;
          @endforeach
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>here's the screenshot of the view table</p>
  
  <p><a href="https://i.stack.imgur.com/CYeXA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CYeXA.png" alt="enter image description here"></a></p>
  
  <p><strong>My index function in my controller</strong></p>
  
  <pre><code>public function index()
  {
      $warehouse1stocks = Warehouse1stocks::all();
      // dd($warehouse1stock-&gt;companies-&gt;comp_name);
      return view('warehouse1.index', compact('warehouse1stocks', $warehouse1stocks));
  }
  </code></pre>
  
  <p>what I'm trying to achieve is to group the product code and sum its 'in'. How
  can i do that? thank you so much in advance!</p>
  
  UPDATE
  
  <p>this is additional information regarding with my inquiry.</p>
  
  <p>The column product name and product code from my output table is from eloquent eloquent orderItems</p>
  
  <pre><code>class Orderitems extends Model
  {
      protected $table = 'order_items';
  
      protected $fillable = [
          'product_id',
          'order_id',
          'product_name',
          'product_code',
          'cost',
          'rate',
          'quantity',
          'total_cost',
      ];
  }
  </code></pre>
  
  <p><strong>Progress Update</strong></p>
  
  <p>@apokrypus, Here's the error after applying your suggestion.</p>
  
  <p><strong>here's the updated code</strong></p>
  
  <pre><code>$warehouse1stocks = Warehouse1stocks::select(
                            'order_item_id',
                            Warehouse1stocks::raw('SUM(stock_in_qty) as stock_in_qty'),
                            Warehouse1stocks::raw('SUM(stock_out_qty) as stock_out_qty')
                      )-&gt;groupBy('order_item_id')-&gt;get();
  // dd($warehouse1stocks);
  return view('warehouse1.index', compact('warehouse1stocks', $warehouse1stocks));
  </code></pre>
  
  <p><strong>Here's the screenshot</strong></p>
  
  <p><a href="https://i.stack.imgur.com/EoAUr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EoAUr.png" alt="enter image description here"></a></p>