gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:03:40 24 4
gpt4 key购买 nike

一次加载所有数据会使页面加载速度变慢。有什么方法可以只加载 10 条记录并在用户单击第 2 页之类的情况下加载其他记录吗?

下面是我用来加载所有数据的代码。它工作得很好,但是当我导入 5,000 条记录时,由于加载缓慢,它使页面崩溃并出现错误 500

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

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

当我使用 paginate(); 时,它只会显示 1 页

最佳答案

您应该使用带参数的分页,如下所示:

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

在 View 中使用它:

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

关于php - 如何对数据表进行分页以便数据不会在 Laravel 中一起加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66541926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com