在Laravel刀片中的foreach内部分组数据

时间:2019-03-13 07:08:08

标签: php laravel

我正在尝试在May表中对产品名称进行分组。我发现了在控制器中放置-> groupBy的建议,但是在执行此操作时,我与其他模型的关系中断了。实现我想要的目标的投注方式是什么?

这是我的视图刀片中的代码。

<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>

这是视图表的屏幕截图

enter image description here

我在控制器中的索引功能

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

我想要实现的是对产品代码进行分组并对其“中”求和。怎么样 我能做到吗?提前非常感谢您!

更新

这是有关我的询问的其他信息。

我的输出表中的列产品名称和产品代码来自雄辩的雄辩的orderItems

class Orderitems extends Model
{
    protected $table = 'order_items';

    protected $fillable = [
        'product_id',
        'order_id',
        'product_name',
        'product_code',
        'cost',
        'rate',
        'quantity',
        'total_cost',
    ];
}

进度更新

@apokrypus,这是应用您的建议后的错误。

这是更新的代码

$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));

以下是屏幕截图

enter image description here

1 个答案:

答案 0 :(得分:1)

假设您想同时求和,那么您可以执行以下操作:

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

(理想情况下)这不要求您进行任何视图更改。

但是,如果每行的交货单都不同,则您可能会考虑不将其包括在结果中,因为在对多行进行分组时将无法显示它。