Having trouble getting pagination to work

What seems to be the problem:
I want to pagination a DB query but getting an error.
Steps to Reproduce:
Functioning query with get().
$this->grid2 = DBName::where(‘field1’, $this->field1)
->where(‘field2’, $this->field2)
->where(‘field3’, $this->field3)
->orderBy(‘field3’, ‘ASC’)
->get();
But when I try to paginate:
$this->grid2 = DBName::where(‘field1’, $this->field1)
->where(‘field2’, $this->field2)
->where(‘field3’, $this->field3)
->orderBy(‘field3’, ‘ASC’)
->paginate(10);
This error shows up:
Livewire component’s [search] public property [grid2] must be of type: [numeric, string, array, null, or boolean]. Only protected or private properties can be set as other types because JavaScript doesn’t need to access them.

Are you using the latest version of Livewire:
yes
Do you have any screenshots or code examples:
As above.

Thanks for any help.

Your public properties need to be JavaScript readable and must be able to be cast as one of the types listed. My guess is that your initial query is able to be cast as an array whereas the pagination cannot (as it exposes a lot of other stuff to make pagination work).

To get around this, you can pass your $this->grid2 into your view upon return. This will make sure that it gets passed around in PHP land vs trying to bridge a gap between JS and PHP.

Component.php

// this is your livewire component code, not your blade code

use WithPagination;

public function render()
{
    $grid2 = DBName::where('field', $this->field)->(...other filtering)->paginate(10);

    return view('livewire.component', ['grid2' => $grid2]); // replace livewire.component with your blade file
}

Thanks for the reply. Unfortunately the same error. But upon running dd($this->grid2) the resulting object is Illuminate\Pagination\LengthAwarePaginator {#1571 :arrow_forward:} Any hints how to pass this to the blade?
Thanks again.

Can you post more of your code? Are you passing down $grid to your view or trying to access it as a public variable? I have a few paginated views that work with the method I’ve posted.

After twisting myself in a knot trying to figure out what the issue was, found this https://github.com/livewire/livewire/issues/722. Basically I had a public property $grid2 which was cause of issue. Working now. Thanks for checking back.