如何在Laravel 5.5中对唯一ID的值进行分组

时间:2019-03-13 15:51:29

标签: php laravel

请查看下表,

enter image description here

我正在尝试按产品代码和产品名称对表进行分组,我的问题是2列来自order_items表

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

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

我正在从该模型中检索记录

class Warehouse1stocks extends Model
{
    protected $table = 'warehouse1';
    protected $fillable = [
        'order_id',
        'product_id',
        'order_item_id',
        'delivery_date',
        'company_id',
        'stock_in_qty',
        'stock_out_qty',
        'transfer_to',
        'delivery_note',
        'user_id'
    ];
}

唯一ID为'warehouse1.order_item_id'='order_items.id'

回到表中,列产品代码和产品名称被调用

{{$warehouse1stock->orderItem->product_code}} 

{{$warehouse1stock->orderItem->product_name}}

这是我到目前为止尝试过的,

$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($warehouse1stocks);
return view('warehouse1.index', compact('warehouse1stocks', $warehouse1stocks));

正确的做法是什么?并同时将stock_in和Stock_out列求和?希望您的代码能提前帮助我!

0 个答案:

没有答案