Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

php - Laravel pagination does not exist

I'm trying to create a pagination, the problem is, is that when I trying to create the pagination I get this error

Method IlluminateDatabaseEloquentCollection::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 AppHttpLivewireCategories;

    use IlluminatePaginationPaginator;
    use LivewireComponent;

    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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You forgot to use the WithPagination trait as stated in the Livewire docs.

<?php

namespace AppHttpLivewireCategories;

use IlluminatePaginationPaginator;
use LivewireComponent;
use LivewireWithPagination;

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...