简体   繁体   中英

Laravel orderBy on with

I have problem with eloquent query. I am using one to Many Relationship to get 'MenuItems' With the 'Menu'. I want to get all menuItems of a particular menu in sorted by 'sort_order' field Using the code below:

Menu::where('slug', 'main-navigation')->with('MenuItems')->orderBy('MenuItems.sort_order', 'asc')->get();

It is not working, is it possible to do it with eloquent? If yes then how?

try this

 Menu::where('slug', 'main-navigation')->with([
     'MenuItems' => function($query) {
          $query->orderBy('MenuItems.sort_order', 'asc')
     }
])->get();

This will sort all the menu-items under each menu as per sort_order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM