Does the method get() and paginate() work in laravel 7.3

67 views Asked by At
<?php

    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use App\Exam;
    use App\Module;
    use App\Dapartment;
    use DB;


    class PastexController extends Controller
    {
    //
    public function index()
    {
        $xams=DB::table('exams')
            ->join('modules', 'modules.id', '=', 'exams.module_code_id')
            ->where('exams.views', '!=', '0')
            ->select('exams.views','modules.name_of_modue', 'modules.code','exams.id','exams.yr', 'exams.month')
            
            ->get()->sortByDesc('views')
       

    ->paginate(7);
        return view('pastex/home', ['xams'=>$xams]);
    }
    }

This is the error message I m getting BadMethodCallException Method Illuminate\Support\Collection::paginate does not exist.

1

There are 1 answers

2
aynber On

Using get() returns a collection, while paginate belongs to the Query Builder. You'll need to use paginate instead of get, and move the ordering to the query builder instead of the collection.

 $xams=DB::table('exams')
            ->join('modules', 'modules.id', '=', 'exams.module_code_id')
            ->where('exams.views', '!=', '0')
            ->select('exams.views','modules.name_of_modue', 'modules.code','exams.id','exams.yr', 'exams.month')
            ->orderByDesc('exams.views')
            ->paginate(7);