Description
How an array of selected users assigned to a public property i.e. $selected = []
, which is defined in a trait i.e. WithBulkActions
and used in the component i.e. UserController
, can be passed to a nested component i.e. UserBulkDeletion
which is using the same trait i.e. WithBulkActions
?
Application structure
- AppHttpLivewire
- Component
- UserController - App/Http/Livewire/Backend/UserManagement/UserController
- UserBulkDeletion - App/Http/Livewire/Backend/UserManagement/LogicalComponent/UserBulkDeletion
- Trait
- WithBulkActions - App/Http/Livewire/Traits/WithBulkActions
Stripped-down, copy-pastable code snippets
class UserController extends Component
{
use WithCachedRows, WithSorting, WithBulkActions, WithPerPagePagination;
public User $user;
public $showFilters = false;
public $filters = [
'search' => "",
'email' => null,
'role' => '',
'date-min' => null,
'date-max' => null,
];
protected $listeners = ['sectionRefresh' => '$refresh'];
public function toggleShowFilters()
{
$this->useCachedRows();
$this->showFilters = ! $this->showFilters;
}
public function resetFilters()
{
$this->reset('filters');
}
public function updatedFilters()
{
$this->resetPage();
}
public function getRowsQueryProperty()
{
$query = User::query()
->when($this->filters['email'], fn($query, $email) => $query->where('email', 'like', '%'.$email.'%'))
->when($this->filters['role'], fn($query, $role) => $query->whereHas('roles', fn ($query) => $query->where('id', $role)))
->when($this->filters['search'], fn($query, $search) => $query->where('name', 'like', '%'.$search.'%'))
->when($this->filters['date-min'], fn($query, $created_at) => $query->where('created_at', '>=', Carbon::createFromFormat('d/m/Y', $created_at)))
->when($this->filters['date-max'], fn($query, $created_at) => $query->where('created_at', '<=', Carbon::createFromFormat('d/m/Y', $created_at)));
return $this->applySorting($query);
}
public function getRowsProperty()
{
return $this->cache(function () {
return $this->applyPagination($this->rowsQuery);
});
}
public function getRolesProperty()
{
$roles = Role::all();
return $roles;
}
public function render()
{
return view('livewire.backend.management.audience-management.user-controller', ['users' => $this->rows, 'roles' => $this->roles]);
}
}
class UserBulkDeletion extends Component
{
use WithCachedRows, WithBulkActions, WithSorting;
public $showUserBulkDeletionModal = false;
public User $user;
public $filters = [
'search' => "",
'email' => null,
'role' => '',
'date-min' => null,
'date-max' => null,
];
protected $listeners = ['deleteUserBulk'];
public function deleteUserBulk()
{
$this->useCachedRows();
$this->showUserBulkDeletionModal = true;
}
public function confirmDeleteBulk()
{
$deleteCount = $this->selectedRowsQuery->count();
foreach ($this->selectedRowsQuery->get() as $queryRow) {
$queryRow->roles()->detach();
}
$this->selectedRowsQuery->delete();
$this->showUserBulkDeletionModal = false;
$this->notify('You've deleted '.$deleteCount.' users');
$this->emit('sectionRefresh');
}
public function getRowsQueryProperty()
{
$query = User::query()
->when($this->filters['email'], fn($query, $email) => $query->where('email', 'like', '%'.$email.'%'))
->when($this->filters['role'], fn($query, $role) => $query->whereHas('roles', fn ($query) => $query->where('id', $role)))
->when($this->filters['search'], fn($query, $search) => $query->where('name', 'like', '%'.$search.'%'))
->when($this->filters['date-min'], fn($query, $created_at) => $query->where('created_at', '>=', Carbon::createFromFormat('d/m/Y', $created_at)))
->when($this->filters['date-max'], fn($query, $created_at) => $query->where('created_at', '<=', Carbon::createFromFormat('d/m/Y', $created_at)));
return $this->applySorting($query);
}
public function render()
{
return view('livewire.backend.management.audience-management.logical-component.user-bulk-deletion');
}
}
trait WithBulkActions
{
public $selectPage = false;
public $selectAll = false;
public $selected = [];
public function renderingWithBulkActions()
{
if ($this->selectAll) $this->selectPageRows();
}
public function updatedSelected()
{
$this->selectAll = false;
$this->selectPage = false;
}
public function updatedSelectPage($value)
{
if ($value) return $this->selectPageRows();
$this->selectAll = false;
$this->selected = [];
}
public function selectPageRows()
{
$this->selected = $this->rows->pluck('id')->map(fn($id) => (string) $id);
}
public function selectAll()
{
$this->selectAll = true;
}
public function getSelectedRowsQueryProperty()
{
return (clone $this->rowsQuery)
->unless($this->selectAll, fn($query) => $query->whereKey($this->selected));
}
}
Context
- Livewire version: 2.3.8
- Laravel version: 8.24.0
- Alpine version: 2.8.0
- Browser: Chrome
question from:
https://stackoverflow.com/questions/65883133/how-a-public-property-which-is-defined-in-a-trait-and-used-in-a-component-can