Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 : Order by in a pivot table

I have a problem with ordering records from a pivot table. I have three tables (many-to-many relationship) like this :

menu
id
name

page
id
name

menu_page
id
name
order
menu_page_id
menu_id
page_id

How can I get all the records from the the pivot table (menu_page) ordered by the "order" attribute using Eloquent ?

like image 755
bidou88 Avatar asked Feb 13 '23 12:02

bidou88


1 Answers

I finally find a way to get what I need with this method in my Menu Controller :

public function menu_page () {

    return $this->belongsToMany('Page', 'menus_pages')->withPivot('id', 'name', 'order', 'menu_page_id')->orderBy('order', 'asc');;

}
like image 73
bidou88 Avatar answered Feb 16 '23 02:02

bidou88