简体   繁体   English

如何对数据表进行分页以使数据不会在 Laravel 中一起加载?

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

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?如果用户单击第 2 页或其他内容,有什么方法可以仅加载 10 条记录并加载其他记录?

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它工作得很好,但是当我导入 5,000 条记录时,由于加载缓慢,它使页面崩溃到错误 500

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

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

when I use paginate();当我使用paginate(); it would only show 1 page它只会显示 1 页

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM