我有两个这样的表:
产品:
+----+-----------+
| id | name |
+----+-----------+
| 1 | Product 1 |
| 2 | Product 2 |
| 3 | Product 3 |
| 4 | Product 4 |
+----+-----------+
Run Code Online (Sandbox Code Playgroud)
价格:
+----+-------+------------+---------------------+
| id | price | product_id | created_at |
+----+-------+------------+---------------------+
| 1 | 20 | 1 | 2014-06-21 16:00:00 |
| 2 | 10 | 1 | 2014-06-21 17:00:00 |
| 3 | 50 | 2 | 2014-06-21 18:00:00 |
| 4 | 40 …
Run Code Online (Sandbox Code Playgroud) 我有这个:
$commentReplies = Comment::whereIn('comment_parent_id', $CommentsIDs)
->take(2)->get();
Run Code Online (Sandbox Code Playgroud)
$CommentsIDs
3个父注释id(1,2,3)的数组在哪里.
我试图检索每个$ commentsID的2个回复(如果它们存在).因此,如果回复存在,则总共有6个回复(每个评论2个)应该返回,仅此而已.但是,在那里使用take(2),它将回复限制为2,并且我们只得到其中一条评论的2个回复.如何设置以最有效的方式为每个注释ID获得2个回复,以及如何使用正确的嵌套在视图中呈现它们?
就像是:
Comment 1
--Comment 1 Reply 1 (load this)
--Comment 1 Reply 2 (load this)
--Comment 1 Reply 3 (don't load this)
--Comment 1 Reply 4 (don't load this)
Comment 2
--Comment 2 Reply 1 (load this)
--Comment 2 Reply 2 (load this)
--Comment 2 Reply 3 (don't load this)
Comment 3
(no replies, don't load anything)
Run Code Online (Sandbox Code Playgroud)
更新:
这是评论模型:
class Comment extends BaseModel {
public function latestTwoComments()
{
return $this->hasMany('Comment','comment_parent_id')->latest()->nPerGroup('comment_parent_id', …
Run Code Online (Sandbox Code Playgroud)