php - Laravel 分页不存在
问题描述
我正在尝试创建分页,问题是,当我尝试创建分页时出现此错误
方法 Illuminate\Database\Eloquent\Collection::pagination 不存在。
我正在使用 laravel 和 livewire。
这是我的代码
$products = $this->category->products->pagination(10);
这是在我的类别模型中
public function products()
{
return $this->hasMany(Product::class);
}
更新
这是我的 livewire 的全部代码
<?php
namespace App\Http\Livewire\Categories;
use Illuminate\Pagination\Paginator;
use Livewire\Component;
class Show extends Component
{
public $category;
public function render()
{
$products = $this->category->products->paginate(10);
return view('livewire.categories.show', ['category' => $this->category, 'products' => $products]);
}
}
和我的 livewire.categories.show 刀片文件
<table class="min-w-full divide-y divide-gray-200">
<tbody>
@foreach($products as $product)
<tr>
<td>
{{ $product->name }}
</td>
</tr>
@endforeach
</tbody>
</table>
<div>
{{ $products->links() }}
</div>
解决方案
您忘记使用Livewire 文档WithPagination
中所述的特征。
<?php
namespace App\Http\Livewire\Categories;
use Illuminate\Pagination\Paginator;
use Livewire\Component;
use Livewire\WithPagination;
class Show extends Component
{
use WithPagination;
public $category;
public function render()
{
$products = $this->category->products()->paginate(10);
return view('livewire.categories.show', ['category' => $this->category, 'products' => $products]);
}
}
推荐阅读
- python - Pandas 绘图条顺序类别
- sql - Oracle DB 中复杂的 SELECT 语句
- html - 如何防止此导航栏崩溃?
- laravel - 如何在 Laravel 5.4 中组合模型和按模型排序两个数据
- reactjs - Webpack+SemanticUI+React:进程未定义
- python - 在散列中使用 .digest()?
- c++ - c ++ CreateThread非静态成员没有特定成员名称
- php - 具有非原始类型提示参数的 Laravel API 端点
- excel - VBA range.formula 问题(LEN、RIGHT 和 LEFT)
- python - 无法在 python 脚本中运行 os.popen