Laravel: eloquent orderBy hasOne 关系列使用 with
Laravel: eloquent orderBy hasOne relation column using with
我有一个模型 Orders
具有 hasOne
关系 participant
。
public function participant()
{
return $this->hasOne('App\OrderParticipant', 'order_id');
}
我需要检索按 participant.last_name
排序的 Orders
集合
我的做法
$orders = \App\Orders::with(['participant',])
->where('user_id', $user->id)
->orderBy('participant.last_name')
->get();
失败:
Undefined table: 7 ERROR: missing FROM-clause entry for table \"participant\"\nLINE 1: ...1
收集后尝试整理
return $orders->sortBy('participant.last_name');
但这根本无法排序
顺便说一句,我正在使用 postgres
谢谢。
不能直接通过hasOne下单,必须使用join
$orders = \App\Orders::with([
'participant',
])
->where('orders.user_id', $user->id)
->join('participants', 'orders.user_id', '=', 'participants.id')
->orderBy('participants.last_name')
->select('orders.*','participants.id','participants.last_name')
->get();
看起来有点多余,不过我用join
整理了一下。虽然很丑。注意 select
部分。没有它,一切都会变得一团糟
$orders = \App\Orders::select('orders.*')
->with([
'....',
'participant',
'participant.documents',
'participant.participantParent',
'participant.country',
'...'
])
->join('order_participants', function ($join) {
$join->on('order_participants.order_id', '=', 'orders.id');
})
->where('orders.user_id', $user->id)
->where(function($q) {
$q->where('orders.status', '!=', 'completed')
->orWhereNull('orders.status');
})
->orderBy('order_participants.last_name')
->orderBy('order_participants.first_name')
->get();
由于我的查询比上面的问题要复杂一些,所以我将整个代码作为示例发布。据我了解,join
必须在 where
语句之前出现
您可以通过以下方式实现:
// eager loading
$orders = \App\Orders::with( [ 'participant'=> function( $q ) {
$q->orderBy( 'last_name', 'asc/desc' );
} ] )->get();
我有一个模型 Orders
具有 hasOne
关系 participant
。
public function participant()
{
return $this->hasOne('App\OrderParticipant', 'order_id');
}
我需要检索按 participant.last_name
Orders
集合
我的做法
$orders = \App\Orders::with(['participant',])
->where('user_id', $user->id)
->orderBy('participant.last_name')
->get();
失败:
Undefined table: 7 ERROR: missing FROM-clause entry for table \"participant\"\nLINE 1: ...1
收集后尝试整理
return $orders->sortBy('participant.last_name');
但这根本无法排序
顺便说一句,我正在使用 postgres
谢谢。
不能直接通过hasOne下单,必须使用join
$orders = \App\Orders::with([
'participant',
])
->where('orders.user_id', $user->id)
->join('participants', 'orders.user_id', '=', 'participants.id')
->orderBy('participants.last_name')
->select('orders.*','participants.id','participants.last_name')
->get();
看起来有点多余,不过我用join
整理了一下。虽然很丑。注意 select
部分。没有它,一切都会变得一团糟
$orders = \App\Orders::select('orders.*')
->with([
'....',
'participant',
'participant.documents',
'participant.participantParent',
'participant.country',
'...'
])
->join('order_participants', function ($join) {
$join->on('order_participants.order_id', '=', 'orders.id');
})
->where('orders.user_id', $user->id)
->where(function($q) {
$q->where('orders.status', '!=', 'completed')
->orWhereNull('orders.status');
})
->orderBy('order_participants.last_name')
->orderBy('order_participants.first_name')
->get();
由于我的查询比上面的问题要复杂一些,所以我将整个代码作为示例发布。据我了解,join
必须在 where
语句之前出现
您可以通过以下方式实现:
// eager loading
$orders = \App\Orders::with( [ 'participant'=> function( $q ) {
$q->orderBy( 'last_name', 'asc/desc' );
} ] )->get();