Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Merklin's avatar

Add current user to collection

Hello. If I am creating a collection like this:

return auth()->user()->teamMembers->pluck('full_name', 'id');

how can I add the current user to it?

This is the relation method:

    public function teamMembers(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'team_user', 'user_id', 'member_id');
    }

and full_name comes from the User model:

    protected function fullName(): Attribute
    {
        return Attribute::make(get: fn (mixed $value, array $attributes) => $this->name . ' ' . $this->last_name);
    }
0 likes
1 reply
tykus's avatar
tykus
Best Answer
Level 104

You can use the union Collection method to append to the Collection while retaining the original keys:

return auth()->user()->teamMembers->pluck('full_name', 'id')
    ->union([auth()->id() => auth()->user()->full_name]);

Otherwise, if the current user should be considered a team member, you can simply add a self-referencing record in the pivot table.

Please or to participate in this conversation.