合并和求和具有相同ID的项目

时间:2018-09-30 15:22:09

标签: php sql laravel

使用Laravel和PHP,我必须合并一个具有相同product_id的项目,才能知道我有多少个仓库。我正在当前仓库中抢购所有产品,然后得到了。

array:3 [▼
  0 => array:5 [▼
    "factor" => -1
    "quantity" => 14
    "product_name" => "Phone"
    "product_id" => 1
    "product_symbol" => "iP"
  ]
  1 => array:5 [▼
    "factor" => 1
    "quantity" => 120
    "product_name" => "Phone"
    "product_id" => 1
    "product_symbol" => "iP"

  ]
  2 => array:5 [▼
    "factor" => -1
    "quantity" => 5
    "product_name" => "Sam Washing Machinge"
    "product_id" => 2
    "product_symbol" => "Sa_DA123WE"

  ]
]

现在在我的视图中,我想显示当前库存状态:

<tbody>
                    @foreach($values as $key => $value)
                        <tr>
                            <td>{{$key+1}}</td>
                            <td>{{$value->product_name}}</td>
                            <td>{{$value->product_symbol}}</td>
                            <td>{{$value->quantity}}</td>
                        </tr>
                    @endforeach
                    </tbody>

问题在于,这令人困惑:

Name: Symbol: Quantity:
Phone iP          14
Phone iP          120
Sam   Sa           5

因此,如您所见,我在Phone中有两个条目,并且必须只有一个条目,因为它是同一项目,因此必须像这样计算:

(foreach products with the same id factorOfTheFirstItem*quantityOfTheFirstItem + factorOfTheSecondItem*quantityOfTheSecondItem and so).

所以最终我的网站必须看起来:

Name: Symbol: Quantity:
Phone iP          106
Sam   Sa           5

控制器:

$values= Document::where('id_warehouse', $warehouseId)
            ->leftJoin('document_items', 'documents.id', '=','document_items.document_id')
            ->leftJoin('products', 'products.id', '=', 'document_items.product_id')
            ->select('documents.factor as factor', 'document_items.quantity as quantity','products.name as product_name','products.id as product_id','products.symbol as product_symbol')

    ->get()->toArray();
return view('admin.inventory.index', compact('values'));

感谢您的帮助! :)

2 个答案:

答案 0 :(得分:1)

根据您最终的期望(以下):

Name: Symbol: Quantity:
Phone iP          106
Sam   Sa           5

您可以轻松地改善查询并获得此结果,类似这样的方法应该起作用:

$documents = Document::where('id_warehouse', $warehouseId)
        ->leftJoin('document_items', 'documents.id', '=','document_items.document_id')
        ->leftJoin('products', 'products.id', '=', 'document_items.product_id')
        ->select(
            DB::raw('SUM(document_items.quantity) as quantity'),
            'products.name as product_name',
            'products.id as product_id',
            'products.symbol as product_symbol'
          )
         ->groupBy('products.name', 'products.id', 'products.symbol')
         ->get();

有了这个,您应该获得具有所需属性的数组。

答案 1 :(得分:1)

不要调用toArray()函数。因为它太容易处理Collections了。所以,

$documents = Document::whateverYourQuery()->get();

// final collection which will hold 'merged data'
$merged_documents = collect();

foreach($documents as $document) {

    // set a new attribute with 'factoredquantity'
    $document->setAttribute('factored_quantity', $document->factor . $document->quantity);

    // set a new attribute with sum of the 'factored quantity' (which is what you need)
    $document->setAttribute('total_quantity', $documents->sum->factored_quantity);

    // set updated 'document' object to the final collection.
    // put function will eliminate duplicates.
    $merged_documents->put($document->id, $document);

}

现在您的每个文档对象中都有一个Total Quantity属性,您可以将其作为$document->total_quantity

进行访问

最后,如果需要,您可以在“ merged_data”集合上调用toArray()函数。