laravel - 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() }}
方法上使用时不起作用。
如果有人解决了这个问题,请帮忙。
提前致谢
解决方案
正如 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
]);
}
推荐阅读
- javascript - javascript:作为参数传递时设置不正确的变量
- c++ - 尝试膨胀 png IDAT 块时出现 Z_DATA_ERROR
- laravel - Laravel 使用 assertJson 测试集合 JSON 响应
- java - 无法在 JavaSpring 中自动装配
- excel - 将一列中的数据重新排列为多行
- c++ - 编译时间常数确定
- php - 如何获取特定字符串后的数据
- sas - 此 SQL 查询的 DATA 步等效项是什么?
- android - 如何从 Firestore 集合中获取所有文档并将它们返回到数组列表中?
- connection - 由于访问问题,无法在 Azure 数据工厂中创建连接