Basically I have three models , Wedding , invites , and users. Weddings has invites and invites has users ( keeping in mind that user can belong to multiple invites ) . I want to access $wedding->users
directly.
Wedding.php
class Wedding extends Model
{
public function invites()
{
return $this->hasMany(Invite::class);
}
...
Invites.php
class Invite extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
...
Tables:
Wedding
- id
- name
Invite
- invite_date
- wedding_id
- user_id
User
- id
- name
- email
And i want to access retrieve users directly , using $wedding->users
What's the relationship between Wedding and users?
Add this method to your Widding class
public function users()
{
return $this->hasManyThrough('App\User', 'App\Invite');
}
for more about hasManyThrough
see the documentation .
It's a BelongsToMany
relationship:
public function users() {
return $this->belongsToMany(User::class, 'invites');
}
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.