Laravel Pagination with get() function

4.8k views Asked by At

This is the controller function. I need to set pagination for this.

public function student_list(){
        $students = Student::orderBy('first_name')->get();
        $stu_na = "Test Page";
        return view('student/student_list', compact('stu_na', 'students'));
}

After reading documentation I tried following ways.

1st Way : $students = Student::orderBy('first_name')->get()->paginate(5);

Error :Method paginate does not exist.

2nd way : $students = Student::orderBy('first_name')->paginate(5)->get();

Error : Type error: Too few arguments to function Illuminate\Support\Collection::get()

How should I use paginate with my controller?

1

There are 1 answers

0
fubar On BEST ANSWER

You don't need ->get(). ->paginate() will execute the query internally itself.

$students = Student::orderBy('first_name')->paginate(5);