Laravel - Relationships - Many To Many Defining The Inverse Of The Relationship
To define the "inverse" of a many-to-many relationship, you should define a method on the related model which also returns the result of the belongsToMany
method. To complete our user / role example, let's define the users
method on the Role
model:
belongsToMany(User::class);
}
}
As you can see, the relationship is defined exactly the same as its User
model counterpart with the exception of referencing the App\Models\User
model. Since we're reusing the belongsToMany
method, all of the usual table and key customization options are available when defining the "inverse" of many-to-many relationships.