Reputation: 1457
Pivot table has one extra field: ordinal for sort order. How can I sort by ordinal and romance_color?
Inventory::where('item_id', '=', $itemId)
->with('size')
->orderBy('romance_color')
->get();
Documentation has the following example:
$users = User::with(array('posts' => function($query)
{
$query->orderBy('created_at', 'desc');
}))->get();
How can I first sort by field in primary table, and then by pivot table field? For example, can I sort by User added date, and then sort by post created date?
Can you suggest alternative solution in eloquent?
Thank you
Upvotes: 2
Views: 447
Reputation: 1457
I used simple join with eloquent:
Inventory::where('item_id', '=', '170')
->with('size', 'color', 'item.category')
->join('attribute_value_size_group as gr', 'gr.attribute_value_id', '=', 'size_id')
->orderBy('romance_color')
->orderBy('ordinal')
->select('inventory.*', 'gr.ordinal')
->get()->toArray();
Upvotes: 1
Reputation: 699
try this one.
$otherModelCollection = Inventory::where('item_id', '=', $itemId)->otherModel()
->orderBy('romance_color')
->orderBy('pivot_ordinal')
->get();
It is somewhat related to this.
http://laravel.io/forum/04-17-2014-order-by-pivot-table-attribute-in-eloquent
Upvotes: 0
Reputation: 3407
You can use Query Builder:
For example:
$pivotTable= DB::table('pivot_table')->where('item_id', $itemID)->orderBy('romance_color')->get();
Upvotes: 0