How to paginate datatables so the datas won't load together in Laravel?

Viewed 87

Loading all the data at once will make the page load slower. Is there any way to load only 10 records and load the others if the user clicks to page 2 or something?

Below is the code I used to load all data. It works just fine but when I import 5,000 records, it crashes the page to error 500 because of the slow loading

public function index()
{
    $users = User::all();
    $documents = Receiving::where('draft', '=', 0)->get();

    return view('documents/index', compact('users', 'documents'));
}

when I use paginate(); it would only show 1 page

1 Answers

You should use paginate with parameter, like this:

$documents = Receiving::where('draft', '=', 0)->paginate(20);

Use this in view:

 <div class="pagination">
      {{ $documents->render() }} or {{ $documents->links() }}
 </div>
Related