问题描述
方法 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 docs 中所述的 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]);
}
}