You have method merge()
https://laravel.com/docs/5.6/collections#method-merge
$all_appts_with_user = $apptsinit->merge($apptsrecpt);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, I'm building an appointments booking system, got a users table and an appointments table. Users can either be the appointment initiator (the one who creates the appointment) or the appointment recipient (the one who receives the appointment request).
I need to display a list of all appointments for a user, where they are either the initiator or the recipient, so I need to join the two separate collections I get, so I can pass that single list to the blade template to display. Using a standard one to many relationship, I have:
// Get all of the user's appointments where they are an initiator: (one to many)
public function appts_initiator()
{
return $this->hasMany('App\Appointment', 'initiator_id');
}
// Get all of the user's appointments where they are a recipient: (one to many)
public function appts_recipient()
{
return $this->hasMany('App\Appointment', 'recipient_id');
}
and controller code:
public function listappointments()
{
// Get appt list for this user (initiator or recipient) from DB:
$apptsinit = User::find(Auth::user()->id)->appts_initiator();
$apptsrecpt = User::find(Auth::user()->id)->appts_recipient();
// NEED TO SOMEHOW JOIN/APPEND $apptsinit AND $apptsrecpt HERE TO FORM ONE COLLECTION:
$all_appts_with_user = ??? // Effectively $apptsinit + $apptsrecpt
// Return view with appts (listed in blade template):
return view('listappts', array('page' => 'listappts', $all_appts_with_user));
}
How do I join the collections please, or is there a different way of achieving this?
Thanks. Alex
Please or to participate in this conversation.