Many-to-many relationship

For the many-to-many relationship, we will have many Bikes in many Garages by using the pivot table.

In the many-to-many relationship, we need to respect some naming rules.

The name of the pivot table should consist of singular names of both tables, separated by underscore symbols, and these names should be arranged in alphabetical order.

By default, there should only be two pivot table fields and the foreign key to each of the tables, in our case, bike_id and garage_id.

Still in the project/app/Bike.app file, let's add the many-to-many relationship between the Bike and Garage models.

Add the following code immediately after the items() function:

public function garages() { return $this->belongsToMany('App\Garage'); ...

Get Hands-On Full Stack Web Development with Angular 6 and Laravel 5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.