Skip to content

Commit 1cf96e0

Browse files
[9.x] Add the orderByPivot method. (laravel#8248)
* Add the orderByPivot method. * Update eloquent-relationships.md Co-authored-by: Taylor Otwell <[email protected]>
1 parent d1cf1df commit 1cf96e0

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

eloquent-relationships.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- [Many To Many Relationships](#many-to-many)
1212
- [Retrieving Intermediate Table Columns](#retrieving-intermediate-table-columns)
1313
- [Filtering Queries Via Intermediate Table Columns](#filtering-queries-via-intermediate-table-columns)
14+
- [Ordering Queries Via Intermediate Table Columns](#ordering-queries-via-intermediate-table-columns)
1415
- [Defining Custom Intermediate Table Models](#defining-custom-intermediate-table-models)
1516
- [Polymorphic Relationships](#polymorphic-relationships)
1617
- [One To One](#one-to-one-polymorphic-relations)
@@ -662,6 +663,15 @@ You can also filter the results returned by `belongsToMany` relationship queries
662663
->as('subscriptions')
663664
->wherePivotNotNull('expired_at');
664665

666+
<a name="ordering-queries-via-intermediate-table-columns"></a>
667+
### Ordering Queries Via Intermediate Table Columns
668+
669+
You can order the results returned by `belongsToMany` relationship queries using the `orderByPivot` method. In the following example, we will retrieve all of the latest badges for the user:
670+
671+
return $this->belongsToMany(Badge::class)
672+
->where('rank', 'gold')
673+
->orderByPivot('created_at', 'desc');
674+
665675
<a name="defining-custom-intermediate-table-models"></a>
666676
### Defining Custom Intermediate Table Models
667677

0 commit comments

Comments
 (0)