【发布时间】:2019-06-10 07:33:21
【问题描述】:
我在子查询(姓氏,名字)中按列排序时遇到问题。
我已经按照其他帖子的建议尝试了此代码:
->with(['customer' => function ($query) {
$query->orderBy("lastname", "asc")
->orderBy("firstname", "asc");
}])
这是我的完整代码,但它不起作用。
return Membership::forCompany($companyId)
->whereIn('state', ['ATTIVA', 'IN ATTESA DI ESITO', 'DA INVIARE'])
->where(function ($query) {
$query->where('end_date', '>=', Carbon::now()->toDateString())
->orWhereNull('end_date');
})
->with('federation')
->with(['customer' => function ($query) {
$query->orderBy("lastname", "asc")
->orderBy("firstname", "asc");
}]);
这里的关系:
在客户模型中我有:
public function memberships() {
return $this->hasMany('App\Models\Membership');
}
在会员模型中我有:
public function customer() {
return $this->belongsTo("App\Models\Customer");
}
【问题讨论】:
-
您不能通过它的子查询对结果应用订单。这是完全错误的做法。 Order by 可以与连接一起使用。如果您想在上述情况下申请order by...您应该从客户表中查询。
-
感谢@VikashPathak,但我有时需要按姓氏(客户表)订购,有时需要按 end_date(会员表)订购。我该如何解决这个迷宫?
-
请分享关系。
-
我在原始帖子中添加了关系。谢谢:)
标签: laravel eloquent sql-order-by