简体   繁体   中英

Grouping data inside foreach in blade in laravel

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?

here's my code in my view blade.

<table class="table table-striped m-b-none" data-ride="datatables" id="table">
    <thead>
        <tr>
            <th width="">Product Code</th>
            <th width="">Product Name</th>
            <th width="">In</th>
            <th width="">Out</th>
            <th width="">Total Stocks</th>
            <th width="">Note</th>
        </tr>
    </thead>
    <tbody>
        @foreach( $warehouse1stocks as $warehouse1stock )
        @php
        $totalStocks = $warehouse1stock->stock_in_qty - $warehouse1stock->stock_out_qty;
        $stockWarning = $warehouse1stock->product->wh1_limit_warning;
        @endphp
        <tr>
            <td>{{$warehouse1stock->orderItem->product_code}}</td>
            <td>{{$warehouse1stock->orderItem->product_name}}</td>
            <td>{{$warehouse1stock->stock_in_qty}}</td>
            <td>{{$warehouse1stock->stock_out_qty}}</td>
            @if($totalStocks <= $stockWarning||$totalStocks==$stockWarning) <td>{{$totalStocks}} <i data-toggle="tooltip" data-placement="top" title="below stock limit, {{$stockWarning}}" class="fas fa-exclamation-circle text-danger"></i></td>
                @else
                <td>{{$totalStocks}}</td>
                @endif
                <td>{{$warehouse1stock->delivery_note}}</td>
        </tr>
        @endforeach
    </tbody>
</table>

here's the screenshot of the view table

在此处输入图片说明

My index function in my controller

public function index()
{
    $warehouse1stocks = Warehouse1stocks::all();
    // dd($warehouse1stock->companies->comp_name);
    return view('warehouse1.index', compact('warehouse1stocks', $warehouse1stocks));
}

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!

UPDATE

this is additional information regarding with my inquiry.

The column product name and product code from my output table is from eloquent eloquent orderItems

 class Orderitems extends Model { protected $table = 'order_items'; protected $fillable = [ 'product_id', 'order_id', 'product_name', 'product_code', 'cost', 'rate', 'quantity', 'total_cost', ]; }

Progress Update

@apokrypus, Here's the error after applying your suggestion.

here's the updated 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') )->groupBy('order_item_id')->get(); // dd($warehouse1stocks); return view('warehouse1.index', compact('warehouse1stocks', $warehouse1stocks));

Here's the screenshot

在此处输入图片说明

Assuming you want to sum both in and out then you can do:

public function index()
{
    $warehouse1stocks =  Warehouse1stocks::select(
                      'order_item_id',
                      \DB::raw('SUM(stock_in_qty) as stock_in_qty'),
                      \DB::raw('SUM(stock_out_qty) as stock_out_qty')
                )->groupBy('order_item_id')
                 ->get()

    // dd($warehouse1stock->companies->comp_name);
    return view('warehouse1.index', compact('warehouse1stocks', $warehouse1stocks));
}

This should (ideally) not require you to make any view changes.

However if your delivery note is different on each row you might consider not including it in the result since you won't be able to show it when grouping multiple rows.

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