From the course: Advanced Laravel
Unlock the full course today
Join today to access over 24,500 courses taught by industry experts.
Many-to-many relationships - Laravel Tutorial
From the course: Advanced Laravel
Many-to-many relationships
- [Instructor] Now we need the feature to book these scheduled classes by members. Each user can book multiple classes and each class can have multiple users. So, this is a many-to-many relationship. We need an intermediate table, here, to store the user ID and the scheduled class ID. intermediate_table, and let me pull that here. In Laravel, when you define a many-to-many relationship by standard convention, this table's name is a combination of both the tables ordered alphabetically. So this would be scheduled_class_user and not unscheduled classes. It has to be singular, _ and not users, again. But this table name doesn't make sense in our case. Instead, let's use a custom name, and let's call it bookings, all right? So here, the first column is, let's have an ID primary key as always. And then the first column would be our user ID, which is a foreign key referencing our user's tables ID column. Okay, and…