微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Laravel Livewire 无法解析模板上的分页关系

如何解决Laravel Livewire 无法解析模板上的分页关系

我在使用带有分页方法的 Relationship-Eloquent 渲染 livewire 模板时遇到问题。

Livewire 组件:

<?PHP

namespace App\Http\Livewire;

use App\Models\User;
use Livewire\Component;
use App\Http\Controllers\AllUsersController;
use Livewire\WithPagination;

class DatatableComponent extends Component
{
    use WithPagination;
    /**
     * @var parent object
     * @property AllUsersController has to be replace using laravel Stubs
     */
    protected $parent = AllUsersController::class;

    /**
     * @var int
     */
    public $perPage = 1;

    /**
     * @var cities
     */
    public $cities;

    /**
     * @var states
     */
    public $states;

    /**
     * @var string
     */
    public $page_title = 'Users';

    /**
     * @var string
     */
    public $page_description = '';

    /**
     * @var array
     */
    public $users;

    /**
     * This used to set the initial value of the parent model.
     *
     * @return default
     */
    public function mount()
    {
        /** @todo initialize data using read method */
        $this->read();
    }

    /**
     * This used to initialize the data .
     * @return initial object
     * @todo  This has to be replaced by the other parent controller objects
     */
    protected function read()
    {
        /** @todo - set all public properties */
        $this->parent = new $this->parent;
        $this->states = $this->parent->getStates();
        $this->cities = $this->parent->getCities();
        $this->users = User::with(['logs' => function($query){
            $query->with('distinctMeta');
        }],'logsMeta','activity')->paginate($this->perPage);
    }

    /**
     * This used to return component.
     *
     * @param void public property
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function render()
    {
        return view('livewire.datatable-component');
    }
}

我遵循的步骤。

当我检查 $this->users 时,它返回用户对象。但在访问模板上的 public $users 属性时,其返回错误

错误

Undefined property: Livewire\CompilerEngineForIgnition::$files

模板:

<div class="card card-custom">
    <div class="card-body">
        {{-- moving form to partials --}}
        @include('livewire._partials.search')
        {{-- end move --}}
        <table class="table table-bordered table-hover">
            <thead>
            <tr>
                <th>{{ __('Email') }}</th>
            </tr>
            </thead>
            <thead>
                <tr>
                    @foreach($users as $index => $user)
                        <td>{{ $user->tualog->email }}</td>
                    @endforeach
                </tr>
            </thead>
        </table>
        {{ $users->links() }}
    </div>
</div>

主要问题 - 没有 toArray() 方法无法使用分页。但是当我在模板 toArray() 上使用 {{ $users->links() }} 时,方法不起作用。

如果有人解决了这个问题,请帮忙解决

提前致谢

解决方法

正如 Remul 所说,您不能将对象分配给 livewire 组件中的公共属性。移除 $users 公共属性,而是在 render() 方法中分配它:

    public function render()
    {
        $users = User::with([
            'logs' => function($query) {
                $query->with('distinctMeta');
            },'logsMeta','activity'
        ])
        ->paginate($this->perPage);

        return view('livewire.datatable-component',[
            'users' => $users
        ]);
    }
,

我已经通过像这样使用本地属性进行收集来解决它。

protected $collection

和分配

$this->collection = $users = User::with([
            'logs' => function($query) {
                $query->with('distinctMeta');
            },'activity'
        ])
        ->paginate($this->perPage);

我已经改变了渲染方法

 public function render()
 {
    return view('livewire.datatable-component',['users' =>$this->collection]);
 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。