sshateri's avatar

Pagination error

im trying to retrive the users on page mount and paginate them using the following code:

public function mount()
    {
        $this->users = User::paginate(16); 
    }

but im getting this error:

Livewire component's [user-search] public property [users] 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.

the plan is to load all of the users using mount on page load and then let the user filter them using a filter form with multiple criteria. the paginator works using this code:

public function render()
    {
        return view('livewire.user-search', [
            'users' => User::paginate(16),
        ])
            ->extends('layouts.app')
            ->section('content');
    }

but I need to use a specific function to filter the results based on the selected criteria. also, the search is not real-time and there is a button to call the search filter function.

0 likes
4 replies
devingray_'s avatar

use Livewire\WithPagination;

class ComponentClass extends Component
{
    use WithPagination;

    public function render()
    {
        return view('livewire.show-posts', [
            'users' => User::paginate(15),
        ])->extends('layout.app')->section('content');
    }
}

With a mount method, make the users public

use Livewire\WithPagination;

class ComponentClass extends Component
{
    use WithPagination;

    public $users;

    public function mount()
    {
       $this->users = User::paginate(15)
    }

    public function render()
    {
        return view('livewire.show-posts')->extends('layout.app')->section('content');
    }
}
sshateri's avatar

the $user is public otherwise how am I supposed to access it from the view?

class UserSearch extends Component
{
    use WithPagination;

    protected $paginationTheme = 'bootstrap';

    public $search;
    public $users;

    public function render()
    {
        return view('livewire.user-search')
            ->extends('layouts.app')
            ->section('content');
    }

    public function mount()
    {
        $this->users = User::paginate(16);
    }
}

yet still getting the following error:

Livewire\Exceptions\PublicPropertyTypeNotAllowedException
Livewire component's [user-search] public property [users] 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.
warpig's avatar

In this instance $users is an object, have you tried using your Model as a reference? like:

$this->users('App\Models\User');

Also see if by making $users a singular word works.

sshateri's avatar

that didn't work. not sure whats the issue.

Please or to participate in this conversation.