laravel - 使用“with”在关系中进行分页
问题描述
我在分页方面遇到了一些麻烦。我只需要用电影取一个类别并对其进行分页。现在我写了一些代码,但我认为它没有优化。
$category = Category::with(['movies' => function ($query) {
$query->orderBy('id', 'desc')->paginate(18);
}])->where('slug', $slug)->first();
$catMoviesPaginate = $category->movies()->paginate(18);
解决方案
你可以通过延迟加载来做到这一点,因为这里我提到了可以帮助你的代码。
$category = Category::where('slug', $slug)->first();
$movies= $category->movies()->paginate(18); //lazy loding.
return view('example', compact('category', 'movies'));
您也可以在视图文件中呈现分页。
@foreach ($movies as $movie)
{{ $movie->id }}
@endforeach
{!! $movies->render() !!}
推荐阅读
- rest - 如何将 API 参数传递给 GCP 云构建触发器
- python - 如何在 Keras API 中将数组列表作为输入
- node.js - MongoDB 检查索引集合上的太多键
- swiftui - 在 SwiftUI 中使用 WCSession 向 Apple Watch 发送消息
- forms - Symfony 4.3 动态形式的标签解决方案与 Doctrine 多对一关联
- android - Parcelable Kotlin 的 Parcelable 对象列表
- reactjs - 将多个组件连接到 React Redux Store
- processing - 如何下载网络库进行处理?
- php - 通过 Laravel/Eloquent 插入 MySQL 行的问题
- android - 如何在主题更改时制作循环显示效果?(就像在 Telegram 或 VK 中一样)