Skip to content

Relations

Marco E edited this page Aug 28, 2020 · 4 revisions
  • We need to add relationships using the Eloquent ORM.
  • In the User model, we need to specify that one meeting can be associated with many users. We do this by using a Many To Many relationship. Many-to-many relationships are defined by writing a method that returns the result of the belongsToMany method:
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * The meetings that belong to the user.
     */
    public function meetings()
    {
        return $this->belongsToMany('App\Meeting');
    }
}
  • We also need to define the inverse of the relationship as one user can have many meetings. This means that we have to define the users method on the Meeting model:
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    /**
     * The users that belong to the role.
     */
    public function users()
    {
        return $this->belongsToMany('App\User');
    }
}
Clone this wiki locally