AlexSteele's avatar

Livewire3 pagination questions

I have a question about livewire pagination. in a fairly traditional livewire component, I had this :

use Livewire\Component;
use Illuminate\Contracts\View\View;
use Livewire\WithPagination;
use App\Models\Service;

class Services extends Component
{
    use WithPagination;
    public $services;

which results as: foreach() argument must be of type array|object, null given

from this:

        @foreach ($services as $service)

so, I changed to : public $services = [];

which results as: Call to a member function links() on array

from this:

        <!-- Pagination Links -->

        {{ $services->links() }}

my render method from the lw component - now I have gone through many iterations of this. it seems like without pagination, everything work ok.

 public function render(): view
    {
        $services = Service::paginate(10);

        return view('livewire.onyx.services', [
            'services' => $services
        ]);
    }

I know there must be a super simple reason or something I am missing. can anyone point me in the right direction?

0 likes
3 replies
LaryAI's avatar
Level 58

It seems like you're encountering issues with Livewire's pagination and the way you're passing the paginated data to your view. The error messages you're seeing are due to the way you're handling the $services variable.

Here's how you can fix the issues:

  1. Remove the public $services; property from your component class. This property is not needed because you will be fetching the services within the render method and passing them directly to the view.

  2. In your render method, you're correctly paginating the Service model. However, you should not assign the paginated result to a local variable $services. Instead, just return it directly to the view.

  3. In your Blade view, you should use the $services variable that is passed from the render method. Since it's a paginated result, you can call the links() method on it to render the pagination links.

Here's the corrected code:

use Livewire\Component;
use Illuminate\Contracts\View\View;
use Livewire\WithPagination;
use App\Models\Service;

class Services extends Component
{
    use WithPagination;

    public function render(): View
    {
        return view('livewire.onyx.services', [
            'services' => Service::paginate(10)
        ]);
    }
}

And in your Blade view, you should loop through the $services variable like this:

@foreach ($services as $service)
    <!-- Display each service -->
@endforeach

<!-- Pagination Links -->
{{ $services->links() }}

With these changes, your Livewire component should work correctly with pagination. Remember that when using Livewire's WithPagination trait, you don't need to manage the pagination state manually; Livewire takes care of it for you. Just make sure to use the variable passed to the view for both displaying the items and rendering the pagination links.

Snapey's avatar
Snapey
Best Answer
Level 122

just delete the public property entirely . You are not using it and it would be inefficient if you did

1 like

Please or to participate in this conversation.