Laravel Livewire 无法解析 eloquent 与模板分页的关系
Laravel Livewire unable to parse eloquent relationship with pagination on template
我在使用分页方法使用 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 组件中的 public 属性。删除 $users
public 属性 并在 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');
},
'logsMeta',
'activity'
])
->paginate($this->perPage);
我已经用
更改了渲染方法
public function render()
{
return view('livewire.datatable-component',['users' =>$this->collection]);
}
我在使用分页方法使用 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 组件中的 public 属性。删除 $users
public 属性 并在 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');
},
'logsMeta',
'activity'
])
->paginate($this->perPage);
我已经用
更改了渲染方法 public function render()
{
return view('livewire.datatable-component',['users' =>$this->collection]);
}