首页 > 解决方案 > Laravel 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() }}方法上使用时不起作用。

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

提前致谢

标签: laraveleloquentlaravel-bladelaravel-livewire

解决方案


正如 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
        ]);
    }

推荐阅读