Skip to content

Commit 6735039

Browse files
author
Shawn McCool
committed
Merge branch 'hotfix/pivot-doc-improvements'
2 parents 52f2b2b + 2ea95ce commit 6735039

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

database/eloquent.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
- [Inserting & Updating Models](#save)
1010
- [Relationships](#relationships)
1111
- [Inserting Related Models](#inserting-related-models)
12-
- [Working With Intermediate Tables](#intermediate-tables)
12+
- [Working With Pivot Tables](#pivot-tables)
1313
- [Eager Loading](#eager)
1414
- [Constraining Eager Loads](#constraining-eager-loads)
1515
- [Setter & Getter Methods](#getter-and-setter-methods)
@@ -229,7 +229,9 @@ You may be wondering: _If the dynamic properties return the relationship and req
229229
<a name="many-to-many"></a>
230230
### Many-To-Many
231231

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:
233235

234236
**Users:**
235237

@@ -265,7 +267,7 @@ Or, as usual, you may retrieve the relationship through the dynamic roles proper
265267

266268
$roles = User::find(1)->roles;
267269

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:
269271

270272
class User extends Eloquent {
271273

@@ -289,24 +291,26 @@ Let's assume you have a **Post** model that has many comments. Often you may wan
289291

290292
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.
291293

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:
293295

294296
$role = new Role(array('title' => 'Admin'));
295297

296298
$user = User::find(1);
297299

298300
$user->roles()->insert($role);
299301

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!
301303

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:
303305

304306
$user->roles()->attach($role_id);
305307

306-
<a name="intermediate-tables"></a>
307-
## Working With Intermediate Tables
308+
<a name="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.
308312

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:
310314

311315
$user = User::find(1);
312316

@@ -319,7 +323,7 @@ Once we have an instance of the pivot table, we can use it just like any other E
319323
//
320324
}
321325

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:
323327

324328
$user = User::find(1);
325329

@@ -336,7 +340,13 @@ Sometimes you may wish to remove all of the record from the intermediate table f
336340

337341
$user->roles()->delete();
338342

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:
346+
347+
$post->comments()->insert($comment, array('visible' => 1));
348+
349+
$user->roles()->attach($role_id, array('active' => 1));
340350

341351
<a name="eager"></a>
342352
## Eager Loading

files.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
#### Moving a $_FILE to a permanent location:
3636

37-
Input::upload('picture', 'path/to/pictures');
37+
Input::upload('picture', 'path/to/pictures/newfile.ext');
3838

3939
> **Note:** You can easily validate file uploads using the [Validator class](/docs/validation).
4040

0 commit comments

Comments
 (0)