【发布时间】:2015-07-29 10:23:34
【问题描述】:
如何使用 whereHas 对查询的返回数据进行排序?在我的查询中,我必须获取 id 存在于 interest 表中的用户数据,但我需要使用 interest 中的 datetime 列对其进行排序。但是,返回查询根本不对数据进行排序。
这是我的代码 sn-ps 以帮助您理解我的问题。
模型(名称:用户)
//***relationship***//
public function interest(){
return $this->belongsTo('Interest','id','interest_by');
}
控制器
$userInterested = User::whereHas('interest',function($q) use ($id) {
return $q->where('interest_on', $id)
->orderBy('datetime');
});
$userQuery = $userInterested->get();
return $userQuery;
【问题讨论】:
-
哦,我设法为我的问题找到了解决方案,在查询生成器上使用 join() 就满足了我的需要。但是,如果您有任何其他解决方案,请随时发布。