在 Laravel 中合并 2 个数据变量

Combine 2 data variable in Laravel

如何在 Laravel 中组合 2 个可变数据。我正在尝试将 $income1 和 $income2 中的数据合并为 $income data show all data from $income1 and $income2

这是我的控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Collection;

use App\Product;
use App\Blok;

class ProductController extends Controller
{
    public function income()
    {
    $products = Product::all();
    $income1 = \App\Pembelian::join('debitur','debitur.id','=','pembelian.debiturID')
    ->join('blok','blok.blok_id', '=', 'debitur.blokID')
    ->join('products','products.id', '=', 'blok.productID')
    ->get();

    $income2 = \App\Pembayaran::join('angsuran','angsuran.id','=','pembayaran.angsurID')
    ->join('debitur','debitur.id','=','angsuran.debiturID')
    ->join('blok','blok.blok_id', '=', 'debitur.blokID')
    ->join('products','products.id', '=', 'blok.productID')
    ->get();

    $income = collect($income1, $income2);

    $income->all();


    return view('income', compact('products', 'income'));
}
}

但它最终只显示 $income1 数据。

使用array_merge

$merge = array_merge($income1, $income2);

最简单的方法是使用

合并

$merged = $income1->merge($income2);

$income = $merged->all();

因此您将能够使用 $income 来引用这两个变量!

我解决了

使用最简单的方法union

$payment =  Pembayaran::select("pembayaran.penyetor","pembayaran.jumlah_bayar","pembayaran.jenis","pembayaran.lsd","pembayaran.tanggal","pembayaran.keterangan","pembayaran.blok_id");

$incomes = Pembelian::select("pembelian.penyetor","pembelian.jumlah_bayar","pembelian.jenis","pembelian.lsd","pembelian.tanggal","pembelian.keterangan","pembelian.blok_id")
        ->union($payment)
        ->orderBy('tanggal', 'desc')
        ->get();