I have two results sets $result1 and $result2 which are returning below results. $result1 contains 1 record and $result2 has 2 records. How can i append $result2 into $result1 at index[1] without hardcoding index? $result1 may have many records so i just want to append at the end.
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
)
)
)
Below is my source code i tried but its not working
$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>";
Both merge and concat return a new collection without overwriting the old one. By doing
$merged = $result1->merge($result2);
or in the case of the code you added,
$merged = $own_leads->concat($employee_leads);
$merged
will contain the new collection, while the original two collections remain the same.