【问题标题】:Laravel livewire pagination error on mountLaravel livewire 安装时分页错误
【发布时间】:2021-05-11 10:36:15
【问题描述】:

我试图在页面装载时检索用户并使用以下代码对其进行分页:

public function mount()
{
     $this->users = User::where('register_completed', '1')->paginate(16); 
}

但我收到此错误:

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.

计划是在页面加载时使用mount 加载所有用户,然后让用户使用具有多个条件的过滤器表单过滤它们。分页器使用以下代码工作:

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

但我需要使用特定函数来根据所选条件过滤结果。此外,搜索不是实时的,并且有一个按钮可以调用搜索过滤器功能。 不知道为什么分页只在通过 render 方法时才有效。 $users 也是一个公共属性,可以从视图中访问它。

【问题讨论】:

  • 请分享您完整的UserSearch Livewire 组件。
  • 这里是<?php namespace App\Http\Livewire; use App\Models\User; use Livewire\Component; use Livewire\WithPagination; 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::where('register_completed', '1')->paginate(16); } }

标签: laravel laravel-livewire


【解决方案1】:

您似乎在 Livewire 组件中输入了 $users 属性。 正如错误消息所说,您不能将公共属性键入提示。除了numericstringarraynullboolean

请参阅Livewire docs 中的重要说明

【讨论】:

  • 或以下 PHP 类型之一:Stringable、Collection、DateTime、Model、EloquentCollection。其中$users 属于 EloquentCollection。对吗?
猜你喜欢
  • 2021-01-21
  • 2014-01-08
  • 2021-05-01
  • 1970-01-01
  • 2021-02-19
  • 1970-01-01
  • 2021-03-05
  • 2020-07-03
  • 1970-01-01
相关资源
最近更新 更多