laravel 按关系列排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38261546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:27:35  来源:igfitidea点击:

Order by relationship column

laraveleloquent

提问by TheUnreal

I have the following query:

我有以下查询:

$items = UserItems::with('item')
        ->where('user_id','=',$this->id)
        ->where('quantity','>',0)
        ->get();

I need to order it by item.type so I tried:

我需要按 item.type 订购,所以我试过:

$items = UserItems::with('item')
        ->where('user_id','=',$this->id)
        ->where('quantity','>',0)
        ->orderBy('item.type')
        ->get();

but I get Unknown column 'item.type' in 'order clause'

但我明白了 Unknown column 'item.type' in 'order clause'

What I am missing?

我缺少什么?

回答by TheUnreal

join() worked fine thanks to @rypskarcomment

由于@rypskar评论,join() 工作正常

$items = UserItems
        ::where('user_id','=',$this->id)
        ->where('quantity','>',0)
        ->join('items', 'items.id', '=', 'user_items.item_id')
        ->orderBy('items.type')
        ->select('user_items.*') //see PS:
        ->get();

PS: To avoid the idattribute (or any shared name attribute between the two tables) to overlap and resulting in the wrong value, you should specify the select limit with select('user_items.*').

PS:为避免id属性(或两个表之间的任何共享名称属性)重叠并导致错误值,您应该使用select('user_items.*').

回答by Repox

Well, your eager loading is probably not building the query you're expecting, and you can check it by enabling the query log.

好吧,您的急切加载可能没有构建您期望的查询,您可以通过启用查询日志来检查它。

But I would probably just use a collection filter:

但我可能只会使用集合过滤器:

$items = UserItems::where('user_id','=',$this->id)
        ->where('quantity','>',0)
        ->get()
        ->sortBy(function($useritem, $key) {
          return $useritem->item->type;
        });

回答by Κουντρ?τ Σπ?ρου-Μαντ?λ

I found another way of sorting a dataset using a field from a related model, you can get a function in the model that gets a unique relation to the related table(ex: table room related to room category, and the room is related to a category by category id, you can have a function like 'room_category' which returns the related category based on the category id of the Room Model) and after that the code will be the following:

我找到了另一种使用相关模型中的字段对数据集进行排序的方法,您可以在模型中获得一个函数,该函数获得与相关表的唯一关系(例如:与房间类别相关的表房间,而房间与按类别 id 分类,你可以有一个像“room_category”这样的函数,它根据房间模型的类别 id 返回相关的类别),然后代码如下:

Room::with('room_category')->all()->sortBy('room_category.name',SORT_REGULAR,false);

This will get you the rooms sorted by category name

这将为您提供按类别名称排序的房间

I did this on a project where i had a DataTable with Server side processing and i had a case where it was required to sort by a field of a related entity, i did it like this and it works. More easier, more proper to MVC standards.

我在一个项目中做了这个,我有一个带有服务器端处理的 DataTable,我有一个案例,需要按相关实体的字段进行排序,我这样做了,并且可以正常工作。更容易,更适合 MVC 标准。

In your case it will be in a similar fashion:

在您的情况下,它将采用类似的方式:

User::with('item')->where('quantity','>',0)->get()->sortBy('item.type',SORT_REGULAR,false);