Sort and filter posts with ajax

Posted 4 weeks ago by slickness

I would like to be able to filter and sort the posts via ajax. I can filter the posts already, but I do not know how to combine sorting with filtering. I have several filters and several sorting options.

ajax works, i only have problems with the controller. I already wrote the querys.

sort:

top posts (if request has "top") ->OrderByVotes() newest posts (if request has "new") ->latest() most commented (if request has "most") ->OrderByComments()

this is my controller:

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);
                }

Please sign in or create an account to participate in this conversation.