I have a problem here. I have to get the channels, and in these channels have field content_id and began_at time of the content belong to this channel.
So the problem is I can get max(began_at)
but I can not get the content_id. How can I get the content_id by max(began_at)
in selectRaw()?
My query:
$channels = \DB::table('channels')
->selectRaw('
channels.*,
max(contents.began_at) as latest_message_time
')
->join('subscriptions', static function (JoinClause $join) use ($device_id) {
$join->on('subscriptions.channel_id', '=', 'channels.channel_id')
->where('device_id', $device_id);
})
->join('contents', static function (JoinClause $join) {
$join->on('contents.channel_id', '=', 'subscriptions.channel_id');
})
->join('content_user', static function (JoinClause $join) {
$join->on('contents.content_id', '=', 'content_user.content_id');
$join->on('subscriptions.user_id', '=', 'content_user.user_id');
})
->orderByDesc('latest_message_time')
->groupBy('channels.channel_id')
->get();
You can't use ->orderByDesc('latest_message_time')
immediately as latest_message_time
field was created on the query. instead of orderByDesc
.
use ->orderByRaw("max(contents.began_at) DESC")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.