向 stdClass 对象添加更多值

Add more values to stdClass Object

我有两个结果集 $result1$result2 返回以下结果。 $result1 包含 1 条记录,$result2 有 2 条记录。我如何在没有硬编码索引的情况下将 $result2 附加到索引 [1] 的 $result1 中? $result1 可能有很多记录,所以我只想在最后追加。

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 10
                    [organisation] => Tyre Manufacturer
                    [creaedtme] => 2022-01-06 02:55:15
                )

        )

)
Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 7
                    [organisation] => A2Z PEST CONTROL
                    [firstprefix] => Ms
                    [creaedtme] => 2022-01-07 07:40:23
                )

            [1] => stdClass Object
                (
                    [id] => 11
                    [organisation] => Spare Parts
            [creaedtme] => 2022-01-06 03:00:06
                )


        )

)

下面是我试过的源代码,但它不起作用

$own_leads = DB::table('leaddata')->where('createdy', $user_id)->orderBy('id','DESC')->get();
// Initialize employee leads
$employee_leads = (object)[];

// If logged in user is State Level Admin then get all leads of that state
if ($user_role == 'State Level Admin') {
    $employee_leads = DB::table('users')
        ->join('leaddata', 'users.id', '=', 'leaddata.createdy')
        ->whereIn('users.stateid', $user_state)
        ->get();
}
echo $count = $own_leads->count();
$merged = $own_leads->concat($employee_leads);
$merged->all();
echo "<pre>";
print_r($own_leads);
echo "</pre>";

两个 merge and concat return 一个新的 collection 没有覆盖旧的。通过

$merged = $result1->merge($result2);

或者对于您添加的代码,

$merged = $own_leads->concat($employee_leads);

$merged 将包含新的 collection,而原来的两个 collection 保持不变。