Laravel 使用 get() 函数分页
Laravel Pagination with get() function
这是控制器函数。我需要为此设置分页。
public function student_list(){
$students = Student::orderBy('first_name')->get();
$stu_na = "Test Page";
return view('student/student_list', compact('stu_na', 'students'));
}
阅读文档后,我尝试了以下方法。
第一种方式: $students = Student::orderBy('first_name')->get()->paginate(5);
Error :Method paginate does not exist.
第二种方式 : $students = Student::orderBy('first_name')->paginate(5)->get();
Error : Type error: Too few arguments to function
Illuminate\Support\Collection::get()
我应该如何将分页与我的控制器一起使用?
您不需要 ->get()
。 ->paginate()
将在内部自行执行查询。
$students = Student::orderBy('first_name')->paginate(5);
这是控制器函数。我需要为此设置分页。
public function student_list(){
$students = Student::orderBy('first_name')->get();
$stu_na = "Test Page";
return view('student/student_list', compact('stu_na', 'students'));
}
阅读文档后,我尝试了以下方法。
第一种方式: $students = Student::orderBy('first_name')->get()->paginate(5);
Error :Method paginate does not exist.
第二种方式 : $students = Student::orderBy('first_name')->paginate(5)->get();
Error : Type error: Too few arguments to function Illuminate\Support\Collection::get()
我应该如何将分页与我的控制器一起使用?
您不需要 ->get()
。 ->paginate()
将在内部自行执行查询。
$students = Student::orderBy('first_name')->paginate(5);