Laravel: OrderBy nested object in collection

血红的双手。 提交于 2021-01-29 09:36:19

问题


My LaravelController returns me an Collection with a nested object. What I need to do is to order it by a property of the nested object. The collection looks like this:

{ID:1,
    subobject:{order:555}
},

{ID:2,
    subobject:{order:444}
},

I want to order the objects by subobject.order (ascending) (ordered->subobject->order)

This is what the Controller currently does:

$ordered = List::with('stuff')->whereIn('ID', $foo)
        ->with('extrastuff')
        ->get();   

The result of this is fine, just not in the order I need it in.

So I tried:

$ordered = List::with('stuff')->whereIn('ID', $foo)
        ->with('extrastuff')
        ->orderBy('object.order','asc')
        ->get();

But that gives me an error:

"Illuminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'subobject.order' in 'order clause' (SQL: select * from `list` where `ID` in (2373, 2374, 2376, 2378, 2379, 2372) order by `subobject`.`order` asc) "

How can I order this?


回答1:


You can sort with a closure after you retrieve your collection. This is more customizable.

$collection = $collection->sort(function ($a, $b) {
    if ($a->subobject->order == $b->subobject->order) {
        return 0;
    }
    return ($a->subobject->order < $b->subobject->order) ? -1 : 1;
});

I believe sortBy also fits your use case and the question you are asking:

$sorted = $collection->sortBy("suboject.order");

This is a simple reference https://riptutorial.com/laravel/example/11490/sorting-a-collection




回答2:


Since I couldn't get it working (no sorting/ordering but also no error messages with @Brighton Ballfrey's solution) I created a workaround by recreating the collection:

Instead of starting with

$collection = List::with('stuff') [..] 

I startetd with the subobject model and got the relations with the other models from there.

$collection = SubobjectModel::with('list') [..]

Like that I have the property to order by in the top level of the collection and orderBy works like a charm.



来源:https://stackoverflow.com/questions/61826730/laravel-orderby-nested-object-in-collection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!