【问题标题】:Sort and filter posts with ajax使用 ajax 对帖子进行排序和过滤
【发布时间】:2019-11-04 16:01:47
【问题描述】:

我希望能够通过 ajax 过滤和排序帖子。我已经可以过滤帖子,但我不知道如何将排序与过滤结合起来。 我有几个过滤器和几个排序选项。

ajax 有效,我只有控制器有问题。我已经写好了查询。

排序:

  • 热门帖子(如果请求有“top”)->OrderByVotes()
  • 最新帖子(如果请求有“新”)->latest()
  • 评论最多(如果请求有“最多”)->OrderByComments()

这是我的控制器:

                if (request()->has('image')) {
                    $postall = $user->posts()->with('comments', 'tags')->whereIn('privacy', [1, 2])->where('type', '=', 4)->where('status', 1)->where('archived', 0)->latest()
                        ->paginate(15)->appends('image', request('image'));
                } elseif (request()->has('video')) {
                    $postall = $user->posts()->with('comments', 'tags')->whereIn('privacy', [1, 2])->where('type', '=', 5)->where('status', 1)->where('archived', 0)->latest()
                        ->paginate(15)->appends('video', request('video'));
                } elseif (request()->has('audio')) {
                    $postall = $user->posts()->with('comments', 'tags')->whereIn('privacy', [1, 2])->where('type', '=', 6)->where('status', 1)->where('archived', 0)->latest()
                        ->paginate(15)->appends('audio', request('audio'));
                } elseif (request()->has('text')) {
                    $postall = $user->posts()->with('comments', 'tags')->whereIn('privacy', [1, 2])->whereIn('type', [1, 2, 3, 8])->where('status', 1)->where('archived', 0)->latest()
                        ->paginate(15)->appends('text', request('text'));
                } else {
                    $postall = $user->posts()->with('comments', 'tags')->whereIn('posts.privacy', [1, 2])->where('posts.type', '!=', 7)->where('posts.status', 1)->where('posts.archived', 0)->latest()->paginate(15);
                }

【问题讨论】:

    标签: ajax laravel sorting filter controller


    【解决方案1】:

    你可以在 laravel 的查询中使用 when 作为条件子句。这是关于它的文档Conditional Clauses

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-10
      • 1970-01-01
      • 1970-01-01
      • 2015-10-29
      • 2018-05-13
      • 2016-11-04
      • 1970-01-01
      相关资源
      最近更新 更多