合并的集合未合并。不论create_at日期如何,所有记录均排在第一位

时间:2019-02-12 11:15:05

标签: laravel

合并的集合未合并。不论create_at日期如何,所有记录均排在第一位。 所有捐赠都首先出现,然后付款才出现第二。它们不是基于created_date作为一个集合来组织的。

$donations = Donation::where('status', 1)->orderBy('created_at', 'desc')->get();

    foreach($donations as $donation){
        $donation->type = 'Donation';
    }

    $coupayments = Payment::where('status', 1)->orderBy('created_at', 'desc')->get();

    foreach($payments as $payment){
        $payment->type = 'Payment';
    }

    $payments = $donations->merge($coupayments);

1 个答案:

答案 0 :(得分:1)

您需要在合并两个集合后进行排序

$donations = Donation::where('status', 1)->get();

foreach($donations as $donation){
    $donation->type = 'Donation';
}

$coupayments = Payment::where('status', 1)->get();

foreach($coupayments as $payment){
    $payment->type = 'Payment';
}

$coupayments = $donations->merge($coupayments);
$payments = $coupayments->sortByDesc('created_at');