I'm trying to create a pagination, the problem is, is that when I trying to create the pagination I get this error
Method Illuminate\Database\Eloquent\Collection::pagination does not exist.
I'm using laravel and livewire.
This is my code
$products = $this->category->products->pagination(10);
This is in my Category model
public function products()
{
return $this->hasMany(Product::class);
}
UPDATE
This is my whole code for my 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]);
}
}
and my livewire.categories.show blade file
<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>
You forgot to use the WithPagination
trait as stated in the Livewire docs.
<?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]);
}
}