You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -229,7 +229,9 @@ You may be wondering: _If the dynamic properties return the relationship and req
229
229
<aname="many-to-many"></a>
230
230
### Many-To-Many
231
231
232
-
Many-to-many relationships are the most complicated of the three relationships. But don't worry, you can do this. For example, assume a User has many Roles, but a Role can also belong to many Users. Three database tables must be created to accomplish this relationship: a **users** table, a **roles** table, and a **role_user** table. The structure for each table looks like this:
232
+
Many-to-many relationships are the most complicated of the three relationships. In this example we have a User model which can have many Roles. But, a Role can also belong to many Users. Three database tables must be created to accomplish this relationship: a **users** table, a **roles** table, and a **role_user** table. We call the **role_user** table the "pivot table."
233
+
234
+
The structure for each table looks like this:
233
235
234
236
**Users:**
235
237
@@ -265,7 +267,7 @@ Or, as usual, you may retrieve the relationship through the dynamic roles proper
265
267
266
268
$roles = User::find(1)->roles;
267
269
268
-
As you may have noticed, the default name of the intermediate table is the singular names of the two related models arranged alphabetically and concatenated by an underscore. However, you are free to specify your own table name. Simply pass the table name in the second parameter to the **has\_and\_belongs\_to\_many** method:
270
+
As you may have noticed, the default name of the pivot table is the singular names of the two related models arranged alphabetically and concatenated by an underscore. However, you are free to specify your own table name. Simply pass the table name in the second parameter to the **has\_and\_belongs\_to\_many** method:
269
271
270
272
class User extends Eloquent {
271
273
@@ -289,24 +291,26 @@ Let's assume you have a **Post** model that has many comments. Often you may wan
289
291
290
292
When inserting related models through their parent model, the foreign key will automatically be set. So, in this case, the "post_id" was automatically set to "1" on the newly inserted comment.
291
293
292
-
This is even more helpful when working with many-to-many relationships. For example, consider a **User** model that has many roles. Likewise, the **Role** model may have many users. So, the intermediate table for this relationship has "user_id" and "role_id" columns. Now, let's insert a new Role for a User:
294
+
This is even more helpful when working with many-to-many relationships. For example, consider a **User** model that has many roles. Likewise, the **Role** model may have many users. So, the pivot table for this relationship has "user_id" and "role_id" columns. Now, let's insert a new Role for a User:
293
295
294
296
$role = new Role(array('title' => 'Admin'));
295
297
296
298
$user = User::find(1);
297
299
298
300
$user->roles()->insert($role);
299
301
300
-
Now, when the Role is inserted, not only is the Role inserted into the "roles" table, but a record in the intermediate table is also inserted for you. It couldn't be easier!
302
+
Now, when the Role is inserted, not only is the Role inserted into the "roles" table, but a record in the pivot table is also inserted for you. It couldn't be easier!
301
303
302
-
However, you may often only want to insert a new record into the intermediate table. For example, perhaps the role you wish to attach to the user already exists. Just use the attach method:
304
+
However, you may often only want to insert a new record into the pivot table. For example, perhaps the role you wish to attach to the user already exists. Just use the attach method:
303
305
304
306
$user->roles()->attach($role_id);
305
307
306
-
<aname="intermediate-tables"></a>
307
-
## Working With Intermediate Tables
308
+
<aname="pivot-tables"></a>
309
+
## Working With Pivot Tables
310
+
311
+
As your probably know, many-to-many relationships require the presence of an intermediate table. In one example case we have a **User** model that has many roles. And, likewise, a **Role** model that has many users. So the intermediate table has "user_id" and "role_id" columns.
308
312
309
-
As your probably know, many-to-many relationships require the presence of an intermediate table. Eloquent makes it a breeze to maintain this table. For example, let's assume we have a **User** model that has many roles. And, likewise, a **Role** model that has many users. So the intermediate table has "user_id" and "role_id" columns. We can access the pivot table for the relationship like so:
313
+
These tables are called **pivot tables**. We can access the pivot table for the relationship like so:
310
314
311
315
$user = User::find(1);
312
316
@@ -319,7 +323,7 @@ Once we have an instance of the pivot table, we can use it just like any other E
319
323
//
320
324
}
321
325
322
-
You may also access the specific intermediate table row associated with a given record. For example:
326
+
You may also access a specific row in the pivot table that is associated with a given record. For example:
323
327
324
328
$user = User::find(1);
325
329
@@ -336,7 +340,13 @@ Sometimes you may wish to remove all of the record from the intermediate table f
336
340
337
341
$user->roles()->delete();
338
342
339
-
Note that this does not delete the roles from the "roles" table, but only removes the records from the intermediate table which associated the roles with the given user.
343
+
Note that this does not delete the roles from the "roles" table, but only removes the records from the pivot table which associated the roles with the given user.
344
+
345
+
If you ever find yourself wanting to add additional data into the pivot table during an **insert** or **attach** you can do so by passing an array of data as the second parameter for both:
0 commit comments