Laravel/Ajax - 加载更多结果而不与请求冲突的新结果

我的网站上有文章,文章有评论。


当有人拉起一篇文章时,默认的评论排序是“新建”。


我的第一个控制器将拉起这篇文章和 10 条评论(我只使用 10 条用于测试目的,实际上它会像 200 条)。


$submission = Submission::where('id', $id)->first();

$comments = Comment::where('submission_id', $submission->id)

            ->where('parent_id', NULL)

            ->orderBy('removed','asc')

            ->orderBy('created_at', 'desc')

            ->paginate(10);

我还有第二个控制器,用于通过 AJAX 加载对文章的更多评论。


public function loadMoreNewComments($submissionID, $skip, Request $request) {


    $submission  = Submission::findOrFail($submissionID);


    if($request->ajax()) {


        $comments = $submission->comments()->orderBy('created_at', 'desc')->skip($skip)->take(10)->get();


        return [

            'replies' => view('partials.comment_loop')->with('submission', $submission)->with('comments', $comments)->render(),

        ];  


    }


}

这将使当您单击“查看更多评论”按钮时,您会获得 10 个以上的结果。


这使用了一个$skip计数,它根据已加载的数量跳过一定数量的结果。因此,由于在此示例中我们一次加载 10,因此第一次$skip计数将为 10,然后第二次将是 20,然后是 30,依此类推,然后将take(10)在跳过的那些之后产生结果。


这是我的刀片视图中的样子:


<div id="comment-thread-container">

@foreach ($comments as $comment)

    @include('partials.comment_block')  

@endforeach

</div>



@if ($comments->total() > 10)

        <span id="viewMoreNewComments" data-submission-id="{{ $submission->id }}" data-skip="10">

            <i class="fa fa-comments" aria-hidden="true"></i>

            View More Comments (<span id="viewComments-count">{{$comments->total() - 10}}</span>)

        </span>

@endif

现在这将工作得很好,直到您有用户在您加载页面后发表评论。


因为当用户在您加载页面后发表评论时,它会将计数设置为 1。


因此,例如,如果您加载一篇文章,并且当您阅读该文章时,又有 3 条评论,如果您单击“查看更多评论”按钮,您将看到 3 条已加载的重复评论。


我不知道如何解决这个问题。一旦我按“最佳分数”实施排序,这将是一个更大的问题,因为评论可以有赞成票和反对票,因为从用户开始阅读文章到他们完成时,评分最高的评论可能会发生巨大变化。


什么是解决这个问题的方法?保留已加载到缓存中的所有评论 ID 的数组,然后在每个请求中传递该数组?或者有更优雅的解决方案吗?


富国沪深
浏览 182回答 1
1回答

ABOUTYOU

+1 对于建议使用自动增量 id 而不是 created_at 的 Matthias S。我建议您在服务器端跟踪 3 件事,因为您可以对多于一列进行排序。列(例如:id/score)、最后一个值和排序顺序(asc/desc)。你的新电话看起来像这样var page = host + "/load-more-comments/" + $submissionID + "/" + $column + "/" + $lastValue + "/" + $sort;因此,您的新电话将如下所示:public function loadMoreNewComments($submissionID, $skip, Request $request) {&nbsp; &nbsp; $submission&nbsp; = Submission::findOrFail($submissionID);&nbsp; &nbsp; if($request->ajax()) {&nbsp; &nbsp; &nbsp; &nbsp; $comments = Comment::where('submission_id', $submission->id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;->orderBy($column, $sort)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;->when(true, function($query) use ($column, $lastValue, $sort) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ($sort === 'desc') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $query->where($column, '<', $lastValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $query->where($column, '>', $lastValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;->take(10);&nbsp; &nbsp; &nbsp; &nbsp; return [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'replies' => view('partials.comment_loop')->with('submission', $submission)->with('comments', $comments)->render(),&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp;&nbsp;&nbsp; &nbsp; }}编辑:由于多个评论可以具有相同的分数,因此当您按分数排序时,您还必须保留所有 id 的数组,其中分数等于最后一个值。将此数组发送到服务器并使用类似于此的代码:&nbsp; &nbsp; &nbsp; &nbsp; $comments = Comment::where('submission_id', $submission->id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;->orderBy($column, $sort)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;->when($column === 'id', function($query) use ($lastValue, $sort) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ($sort === 'desc') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $query->where('id', '<', $lastValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $query->where('id', '>', $lastValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;})->when($column !== 'id', function($query) use ($column, $lastValue, $sort, $arrayOfIds) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ($sort === 'desc') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $query->where($column, '<=', $lastValue)->whereNotIn('id', $arrayOfIds);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $query->where($column, '>=', $lastValue)->whereNotIn('id', $arrayOfIds);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;})->take(10);
打开App,查看更多内容
随时随地看视频慕课网APP