如何使用collection map对laravel 7 eloquent中的数据进行排序?
How to sort data in laravel 7 eloquent using collection map?
我有安心的代码来获取评论和回复。我想知道如何对回复进行排序:
$comments = Comment::where([
['commentable_type', 'App\Models\Posts\Post'],
['commentable_id', $post->id],
['parent_id', null]
])->with('replies')
->orderBy('created_at', 'desc')
->take(10)
->get()->map(function($comments) {
$comments->setRelation('replies', $comments->replies->take(3));
return $comments;
});
我试过这样做:
$comments = Comment::where([
['commentable_type', 'App\Models\Posts\Post'],
['commentable_id', $post->id],
['parent_id', null]
])->with('replies')
->orderBy('created_at', 'desc')
->take(10)
->get()->map(function($comments) {
$comments->setRelation('replies', $comments->replies->take(3));
$comments->setRelation('replies', $comments->replies)->orderBy('created_at', 'desc');
return $comments;
});
还有
$comments = Comment::where([
['commentable_type', 'App\Models\Posts\Post'],
['commentable_id', $post->id],
['parent_id', null]
])->with('replies')
->orderBy('created_at', 'desc')
->take(10)
->get()->map(function($comments) {
$comments->setRelation('replies', $comments->replies->take(3)->orderBy('created_at', 'desc'));
return $comments;
});
但是没有用。
你能帮帮我吗?
此致
假设您想要每条评论的最后三个回复 - 这应该可行
$comments = Comment::where([
['commentable_type', 'App\Models\Posts\Post'],
['commentable_id', $post->id],
['parent_id', null]
])->with(['replies' => function($query){
$query->latest();
}])
->get()
->map(function($comment) {
$replies = $comment->replies;
unset($comment->replies);
$comment->setRelation('replies', $replies->take(3));
return $comment;
});
我有安心的代码来获取评论和回复。我想知道如何对回复进行排序:
$comments = Comment::where([
['commentable_type', 'App\Models\Posts\Post'],
['commentable_id', $post->id],
['parent_id', null]
])->with('replies')
->orderBy('created_at', 'desc')
->take(10)
->get()->map(function($comments) {
$comments->setRelation('replies', $comments->replies->take(3));
return $comments;
});
我试过这样做:
$comments = Comment::where([
['commentable_type', 'App\Models\Posts\Post'],
['commentable_id', $post->id],
['parent_id', null]
])->with('replies')
->orderBy('created_at', 'desc')
->take(10)
->get()->map(function($comments) {
$comments->setRelation('replies', $comments->replies->take(3));
$comments->setRelation('replies', $comments->replies)->orderBy('created_at', 'desc');
return $comments;
});
还有
$comments = Comment::where([
['commentable_type', 'App\Models\Posts\Post'],
['commentable_id', $post->id],
['parent_id', null]
])->with('replies')
->orderBy('created_at', 'desc')
->take(10)
->get()->map(function($comments) {
$comments->setRelation('replies', $comments->replies->take(3)->orderBy('created_at', 'desc'));
return $comments;
});
但是没有用。
你能帮帮我吗?
此致
假设您想要每条评论的最后三个回复 - 这应该可行
$comments = Comment::where([
['commentable_type', 'App\Models\Posts\Post'],
['commentable_id', $post->id],
['parent_id', null]
])->with(['replies' => function($query){
$query->latest();
}])
->get()
->map(function($comment) {
$replies = $comment->replies;
unset($comment->replies);
$comment->setRelation('replies', $replies->take(3));
return $comment;
});