Laravel Livewire 无法解析模板上分页的雄辩关系
Posted
技术标签:
【中文标题】Laravel Livewire 无法解析模板上分页的雄辩关系【英文标题】:Laravel Livewire unable to parse eloquent relationship with pagination on template 【发布时间】:2021-05-07 18:40:48 【问题描述】:我在使用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()
方法无法使用分页。但是当我在模板 $users->links()
上使用toArray()
时,方法不起作用。
如果有人解决了这个问题,请帮忙。
提前致谢
【问题讨论】:
您不能将LengthAwarePaginator
的实例分配给 Livewire 中的公共属性。 Here 你可以找到允许的类型。您可以做的是将其传递给 render
方法中的视图。
感谢@Remul,我已经修复了它,为本地类变量赋值。
【参考方案1】:
正如 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
]);
【讨论】:
【参考方案2】:我已经通过使用本地属性进行这样的收集来解决它。
protected $collection
和分配
$this->collection = $users = User::with([
'logs' => function($query)
$query->with('distinctMeta');
,
'logsMeta',
'activity'
])
->paginate($this->perPage);
我改变了渲染方法
public function render()
return view('livewire.datatable-component',['users' =>$this->collection]);
【讨论】:
以上是关于Laravel Livewire 无法解析模板上分页的雄辩关系的主要内容,如果未能解决你的问题,请参考以下文章
当我在 Laravel livewire 上单击事件时获得 404 模态
dompdf 没有从 Laravel 8 和 Livewire 视图下载 PDF