29.07.2016 Views

laravel-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Eloquent: Relationships 453<br />

In addition to customizing the name of the joining table, you may also customize the column names<br />

of the keys on the table by passing additional arguments to the belongsToMany method. The third<br />

argument is the foreign key name of the model on which you are defining the relationship, while<br />

the fourth argument is the foreign key name of the model that you are joining to:<br />

.<br />

1 return $this->belongsToMany('App\Role', 'user_roles', 'user_id', 'role_id');<br />

Defining The Inverse Of The Relationship<br />

To define the inverse of a many-to-many relationship, you simply place another call to belongsToMany<br />

on your related model. To continue our user roles example, let’s define the users method on the<br />

Role model:<br />

.<br />

1

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!