Laravel分页不存在

Laravel pagination does not exist

我正在尝试创建分页,问题是当我尝试创建分页时出现此错误

Method Illuminate\Database\Eloquent\Collection::pagination does not exist.

我正在使用 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 blade 文件

<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]);
    }
}