简体   繁体   中英

How to use hasManyThrough relation in Laravel

I have problem with hasManyThrough relation in my Laravel project.

I want to get all people connected with selected Event.

My tables looks like:

Events:
- id
- name
- ...

Tickets:
- id
- event_id
- person_id
- ...

People
- id
- first_name
- ...

And in my Event model:

public function people()
{
    return $this->hasManyThrough(Person::class, Ticket::class);
}

So, how to get all people? eg.

Event::first()->people

you can do something like this in the people model

public function events(){
return $this->hasMany('App\Events');
}

and in the event table

public function people(){
return $this->belongsTo('App\people');
}

to get all the people you can do this $people = people::all(); and the events $people->events->name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM