WallyJ's avatar

Livewire Pagination, using mount()

I have followed the instructions in the docs here: https://laravel-livewire.com/docs/mount-method/

to get the results I want from my DB.

Livewire Controller

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Task;
use App\Contact;
use App\Contactnote;

class Contactshow extends Component
{

    public $contacts;
    public $tasktext;
    public $dealId;
    public $taskduedate;
    public $deal_id;

    public function mount($contacts)
    {
        $this->contacts = $contacts;
    }

    public function render()
    {
        return view('livewire.contactshow');
    }

}

View section of results:

<div class="col-md-12">
        <ul class="list-group">
            @foreach($this->contacts->contactnotes->sortByDesc('created_at') as $contactnote)
            <li class="list-group-item">
           {{$contactnote->created_at}} - {{$contactnote->contactnotetext}}
           </li>
            @endforeach
       </ul>
</div>

Results currently show correctly.

Now I want to paginate the results, but the paginate example in the docs:

https://laravel-livewire.com/docs/pagination/

does not use the mount() command in their example, so I am a bit confused. Not sure how to paginate at this point still using mount().

0 likes
9 replies
bugsysha's avatar

Just like having a Vue component that gets props passed to it. You do not paginate inside of that component but from the blade file or Livewire component which is passing that contact data to it. Confusion is that you are using singular component example for the plural purposed component.

Snapey's avatar

you are trying to paginate contacts or contact notes?

WallyJ's avatar

Trying to paginate the contact notes

Snapey's avatar

and contacts is itself a collection that you iterate over?

WallyJ's avatar

This is for a Contacts view, showing the comments as a collection. One contact on this particular view.

Snapey's avatar

one contact? Then why is the variable called $contacts?

get you comments as a separate query and paginate that. This is just understanding pagination - not livewire specific

WallyJ's avatar

I was using eloquent relationships to bring in the contact notes. But it seems that you can’t paginate off of a relationship, only from a query. Is that correct?

Snapey's avatar
Snapey
Best Answer
Level 122

Yes. You cannot pagination a child.

If you have $contact as a single model then

$notes= $contact->contactnotes()->paginate();

Pass this $notes to the view and iterate on it

1 like
WallyJ's avatar

That makes sense. I learned so much about relationships that I try to make it work with everything. :)

Thanks for clarifying.

Please or to participate in this conversation.