Laravel如何合并2个收藏集 [英] Laravel how to merge 2 collections
问题描述
我有2个收藏集,如下所示:
I have 2 collections as below:
$unpaid = WhmcsClientsInvoice::join('quotations', 'quotations.id', 'whmcs_clients_invoices.quotation_id')
->select('whmcs_clients_invoices.invoice_id AS id', 'whmcs_clients_invoices.invoice_date AS date', 'quotations.total AS amount', 'whmcs_clients_invoices.payment_due_date AS due_date')
->where('whmcs_clients_invoices.whmcs_client_id', $client_id)
->where('whmcs_clients_invoices.status', '!=', 'Cancel')
->whereBetween('whmcs_clients_invoices.invoice_date', [$start, $end])
->orderBy('date', 'asc')
->get();
foreach($unpaid as $un) { $un['type'] = "invoice"; }
$paid = InvoiceHistory::join('whmcs_clients_invoices', 'whmcs_clients_invoices.id', 'invoice_history.whmcs_clients_invoices_id')
->select('invoice_history.id', 'invoice_history.date_paid AS date', 'invoice_history.amount_paid AS amount', 'invoice_history.created_at AS due_date')
->where('whmcs_clients_invoices.whmcs_client_id', $client_id)
->where('whmcs_clients_invoices.status', '!=', 'Cancel')
->whereBetween('invoice_history.date_paid', [$start, $end])
->orderBy('date', 'asc')
->get();
foreach ($paid as $paid) { $paid['type'] = 'payment'; }
$trans = $unpaid->merge($paid);
dd($trans);
但是当我尝试合并这两个集合时,将发生错误.
But when I try to merge these 2 collections, an error will occur.
我尝试了 $ transaction = $ unpaid-> union($ paid)-> sortBy('date');
,但无济于事.合并后的集合的资料显示:
I tried $transaction = $unpaid->union($paid)->sortBy('date');
but to no avail. Vardump of the merged collection shows:
error_log $ transaction将显示如下内容:
error_log the $transaction would show something like this:
{日期":"2021-01-2600:00:00","id":12,金额":111.3,到期日期":"2021-01-26"09:00:05",类型":付款","0":{"id":1,日期":"2021-01-07",金额":222.6,到期日期":"2021-01-14",类型":发票"},"1":{"id":3,日期":"2021-01-09",金额":6572,到期日期":"2021-01-16",类型":发票"},"2":{"id":4,日期":"2021-01-12",金额":148.4,到期日期":"2021-01-19",类型":发票"},"3":{"id":5,"date":"2021-01-12","amount":144.16,"due_date":"2021-01-19","type":"invoice";},"4":{"id":6,日期":"2021-01-16",金额":24.38,到期日期":"2021-01-23",类型":发票"},"5":{"id":2,日期":quot; 2021-01-29",金额":222.6,到期日期":"2021-01-14",类型":发票"},"6":{"id";:24,日期":"2021-02-23",金额":190.8,到期日期":"2021-03-02",类型":发票"}}
{"date":"2021-01-26 00:00:00","id":12,"amount":111.3,"due_date":"2021-01-26 09:00:05","type":"payment","0":{"id":1,"date":"2021-01-07","amount":222.6,"due_date":"2021-01-14","type":"invoice"},"1":{"id":3,"date":"2021-01-09","amount":6572,"due_date":"2021-01-16","type":"invoice"},"2":{"id":4,"date":"2021-01-12","amount":148.4,"due_date":"2021-01-19","type":"invoice"},"3":{"id":5,"date":"2021-01-12","amount":144.16,"due_date":"2021-01-19","type":"invoice"},"4":{"id":6,"date":"2021-01-16","amount":24.38,"due_date":"2021-01-23","type":"invoice"},"5":{"id":2,"date":"2021-01-29","amount":222.6,"due_date":"2021-01-14","type":"invoice"},"6":{"id":24,"date":"2021-02-23","amount":190.8,"due_date":"2021-03-02","type":"invoice"}}
很明显,联合会合并了两个集合,但是无法将键分配给第一项...如何合并2个集合而不丢失两个集合中的任何记录?预期的输出应该是这样的:
Apparently the union merge the 2 collections, but couldn't assign key to the first item... How do I merge 2 collections without missing any records in both collections? The expected output should be from this:
Collection {#595 ▼
#items: array:3 [▼
0 => WhmcsClientsInvoice {
"id" = 37
"date" => "2021-02-07 00:00:00"
"amount" => 50.0
"due_date" => "2021-02-07 14:19:20"
"type" => "payment"
}
2 => InvoiceHistory {
"id" = 37
"date" => "2021-02-07 00:00:00"
"amount" => 50.0
"due_date" => "2021-02-07 14:19:20"
"type" => "payment"
}
1 => WhmcsClientsInvoice {
"id" = 37
"date" => "2021-02-07 00:00:00"
"amount" => 50.0
"due_date" => "2021-02-07 14:19:20"
"type" => "payment"
}
]
}
推荐答案
经过几天的艰苦研究,我找到了一种对我有效的方法.这是我更新的代码:
After a few days of painstaking research, I found a way that works for me. This is my updated code:
$transaction = collect(new WhmcsClientsInvoice);
$unpaid = WhmcsClientsInvoice::join('quotations', 'quotations.id', 'whmcs_clients_invoices.quotation_id')
->select('whmcs_clients_invoices.invoice_id', 'whmcs_clients_invoices.invoice_num', 'whmcs_clients_invoices.invoice_date AS date', 'quotations.total AS amount', 'whmcs_clients_invoices.payment_due_date AS due_date')
->where('whmcs_clients_invoices.whmcs_client_id', $client_id)
->where('whmcs_clients_invoices.status', '!=', 'Cancel')
->whereBetween('whmcs_clients_invoices.invoice_date', [$start, $end])
->orderBy('date', 'asc')
->get();
foreach($unpaid as $un) {
$un['type'] = "invoice";
$transaction->push($un);
}
$paid = InvoiceHistory::join('whmcs_clients_invoices', 'whmcs_clients_invoices.id', 'invoice_history.whmcs_clients_invoices_id')
->select('invoice_history.date_paid AS date', 'invoice_history.remark', 'invoice_history.amount_paid AS amount', 'invoice_history.created_at AS due_date')
->where('whmcs_clients_invoices.whmcs_client_id', $client_id)
->where('whmcs_clients_invoices.status', '!=', 'Cancel')
->whereBetween('invoice_history.date_paid', [$start, $end])
->orderBy('date', 'asc')
->get();
foreach ($paid as $pa) {
$pa['type'] = "payment";
$transaction->push($pa);
}
$transaction = $transaction->sortBy('date');
首先,我基于WhmcsClientInvoices创建了一个名为Transaction的空集合.基本上,我的想法是创建2个单独的集合,然后将它们合并到Transaction中.因此,我雄辩了2个雄辩者,以获取2个不同的收藏集.然后,在foreach期间,我添加了一个具有关键调用类型的新项目,以区分每条记录是发票还是付款.然后,我将每个单独的记录收集起来,并将其推送到Transaction.我用$ paid收款重复此过程.最后,我按日期对集合进行排序.
First, I make an empty collection based on WhmcsClientInvoices called Transaction. Basically, my idea is to create 2 separate collections, then merge them into Transaction. So, I made 2 eloquents to get 2 different collections. Then, during the foreach, I added a new item with key call type, to differentiate whether each record is invoice or payment. Then, I take each individual record in collection and push it to Transaction. I repeat the process with $paid collection. Lastly, I sort the collection by date.
这篇关于Laravel如何合并2个收藏集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!