how to paginate and sort results?

Posted 3 months ago by michalis

I have an issue, If I use sortByDesc, the then $files->links() wont work in blade

        $files = File::where('campaign_id', $id);

        if ($month = request('month')) {
            $files->whereMonth('created_at', Carbon::parse($month)->month);
        }

        if ($year = request('year')) {
            $files->whereYear('created_at', $year);
        }

        $files = $files->paginate(2)->sortByDesc('created_at');

when I go to the view I get

Method Illuminate\Database\Eloquent\Collection::links does not exist.

if I do the opposite

 $files = $files->sortByDesc('created_at')->paginate(2);

I get

Call to undefined method Illuminate\Database\Eloquent\Builder::sortByDesc()

is it possible to paginate as well as sort the items?

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