Laravel雄辩的关系与来自两个模型的属性分组

时间:2018-07-23 12:10:57

标签: php laravel laravel-5 laravel-5.6

我有两个模型InvoiceHeaderInvoiceDetail InvoiceHeader 具有以下属性(除其他属性外):idpos_idpos_descriptiondate InvoiceDetail 具有以下属性(除其他属性外):idinvoice_header_idpctprice。两种模型之间存在一对多关系。

我想按pos_id的{​​{1}}和date以及InvoiceHeader的{​​{1}}进行分组。有了这个,我想得到价格的总和。

从我发现的结果来看,不可能同时对两个模型进行分组,因为没有使用Eloquent进行实际的联接。因此,我认为可以按pct上的InvoiceDetail属性进行分组。我做这样的事情:

pct

我被困在这里。我需要在InvoiceDetail上按$invoices = \App\InvoiceHeader::with(['details' => function ($query) { $query->select('pct') ->selectRaw('SUM(price) AS price') ->groupBy(['invoice_header_id', 'pct']); }]) ->whereBetween('date', ['2018-01-01', '2018-07-31']) ->select(['pos_id', 'pos_description', 'date']) ->get(); pos_id分组,在pos_description上按InvoiceHeader分组。

从这里开始,我正在走一条路线,我认为这不是最好/最简单的路线。我有一个雄辩的 InvoiceHeader 集合,并与其 InvoiceDetail 集合有关系。我觉得我应该为此工作,但是由于无法弄清楚如何将其转换为Laravel系列。由于它是唯一使我与所获得的东西更加接近的人,因此我认为可以继续进行下去。但是,如果有人有更好的建议,请分享。在那之前,这就是我得到的。

我从Eloquent集合中制作了Laravel集合,例如:

pct

这使我想到

InvoiceDetail

这是我想去的地方,或者类似的地方:

$collection = collect();
foreach ($invoices as $invoice) {
    foreach ($invoice->details as $detail) {
        $collection->push([
            'pos_id' => $invoice->pos_id,
            'pos_description' => $invoice->pos_description,
            'date' => $invoice->date,
            'pct' => $detail->pct,
            'price' => $detail->price,
        ]);
    }
}

我尝试了laravel docs中的许多功能,并在集合上使用了$collection = [ [ 'pos_id' => 1, 'pos_description' => 'Somewhere', 'date' => '2018-02-28', 'pct' => 6, 'price' => 68.94 ], [ 'pos_id' => 1, 'pos_description' => 'Somewhere', 'date' => '2018-02-28', 'pct' => 21, 'price' => 99.99 ], [ 'pos_id' => 1, 'pos_description' => 'Somewhere', 'date' => '2018-02-28', 'pct' => 21, 'price' => 82.64 ], [ 'pos_id' => 1, 'pos_description' => 'Somewhere', 'date' => '2018-03-31', 'pct' => 21, 'price' => 431.45 ] ] $newCollection = [ [ 'pos_id' => 1, 'pos_description' => 'Somewhere', 'date' => '2018-02-28', 'details' => [ [ 'pct' => 6, 'price' => 68.94 ], [ 'pct' => 21, 'price' => 182.63 ] ] ], [ 'pos_id' => 1, 'pos_description' => 'Somewhere', 'date' => '2018-03-31', 'details' => [ [ 'pct' => 21, 'price' => 431.45 ] ] ] ] each。也来自this link。但我似乎无法获得想要完成的目标。我觉得我缺少一些简单的步骤,这些步骤使我的工作变得非常困难。

免责声明:大多数代码都是松散地基于有效代码。为了简化事情,我简化了一些事情。

1 个答案:

答案 0 :(得分:0)

这样的基本迭代怎么样?

$dirtyColl = [];
foreach ($collection as $key => $value) {
    $dirtyColl[$value['date']]['pos_id'] = $value['pos_id'];
    $dirtyColl[$value['date']]['pos_description'] = $value['pos_description'];
    $dirtyColl[$value['date']]['date'] = $value['date'];
    $dirtyColl[$value['date']]['details'][$value['pct']]['pct'] = $value['pct'];
    $dirtyColl[$value['date']]['details'][$value['pct']]['price'] += $value['price'];
}

print_r(array_values($dirtyColl));

结果

Array
(
    [0] => Array
        (
            [pos_id] => 1
            [pos_description] => Somewhere
            [date] => 2018-02-28
            [details] => Array
                (
                    [6] => Array
                        (
                            [pct] => 6
                            [price] => 68.94
                        )

                    [21] => Array
                        (
                            [pct] => 21
                            [price] => 182.63
                        )

                )

        )

    [1] => Array
        (
            [pos_id] => 1
            [pos_description] => Somewhere
            [date] => 2018-03-31
            [details] => Array
                (
                    [21] => Array
                        (
                            [pct] => 21
                            [price] => 431.45
                        )

                )

        )

)

也许再次将array_values用于“细节”。