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

steve_laracasts's avatar

Vue Eloquent One to Many Foreign Data

This one has me stumped:

I have a Vue instance that is retrieving projects from a database all okay!

Each project is associated with a user and a team:

$projects = auth()->user()->currentTeam()->projects;

Works great - except it doesn't return the Team Name with the data, only the team ID.

How do I change

    public function projects() {
        return $this->hasMany(Project::class, 'team_id');
    }

to make it return the team name as well as the ID with each project.

I have searched a lot but none of the examples I have found seem to relate to the auth() way of doing this retrieval.

Any help very much appreciated. Thank you!

0 likes
1 reply
steve_laracasts's avatar
Level 7

Haha, classic - I just did it:

return $this->hasMany(Project::class, 'team_id')->with('team');

I am sure I tried this - I think I might have been missing the declaration in my project model

    public function team()
    {
        return $this->belongsTo(Team::class);
    }

Anyway - all good - thank you for reading my silly posts - hope it helps someone else :)

Please or to participate in this conversation.