【发布时间】: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