Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple "has many through" with eloquent of laravel

I came accros a problem with laravel's ORM, eloquent and found no solution yet.

I have some tables as follows

Team

 - id
 - name

User

 - id
 - name
 - role
 - team_id

Student_Info

 - id
 - user_id
 - data1
 - data2
 - etc ...

Project

 - id
 - student_id
 - name

Now, I want to query all projects a certain team, where team = 'some team'

Now the thing here is, without an ORM, it's simple, I would have done multiple joins in raw SQL.

However, because all these tables have a common column "name" I will have to alias all this stuff, which is really boring

With eloquent I can't find a way to do this query using "has many through" because it only allows on intermediate and I can't do a raw SQL as the alis thing is really a pain in the ass and as it would be very difficult to map the result to laravel's Models

like image 560
darkylmnx Avatar asked Sep 15 '25 08:09

darkylmnx


1 Answers

There is no native relationship for this case.

I created a HasManyThrough relationship with unlimited levels: Repository on GitHub

After the installation, you can use it like this:

class Team extends Model {
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public function projects() {
        return $this->hasManyDeep(Project::class, [User::class, StudentInfo::class],
            [null, null, 'student_id']);
    }
}

$projects = Team::where('name', 'some team')->first()->projects;
like image 180
Jonas Staudenmeir Avatar answered Sep 17 '25 21:09

Jonas Staudenmeir