Make a separate livewire component to delete an array of users along with traits

对着背影说爱祢 提交于 2021-01-29 15:45:00

问题


Description

I have a "belongsToMany" eloquent relationship between users and roles. I have created a trait for bulk actions, so I'm able to delete users in bulk. Now what I need to do here is, I need to make a separate livewire component to delete users in bulk. But the problem here is that the trait which is being used in Livewire/Backend/UserController needs rowsQuery property, which is present in the UserController and as whenever the users are selected to be deleted, it calls the selectedRowsQuery property from the trait and deletes the user using the following code, $this->selectedRowsQuery->delete();. How can I do the same above function using a separate livewire component.

Stripped-down, copy-pastable code snippets

  • Livewire/Backend/UserController
use App\Http\Livewire\Traits\DataTable\WithBulkActions;

use App\Models\User;

class UserController extends Component
{
    use WithBulkActions;

    public User $user;
    public $filters = [
        'search' => "",
        'email' => null,
        'role' => '',
        'date-min' => null,
        'date-max' => null,
    ];

    protected $listeners = ['sectionRefresh' => '$refresh'];
    
    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 render()
    {
        return view('livewire.backend.management.audience-management.user-controller', ['users' => $this->rows]);
    }
}
  • Livewire/Traits/WithBulkActions
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));
    }
}
  • Livewire/Backend/UserBulkDeletion
use App\Http\Livewire\Traits\DataTable\WithCachedRows;

class UserBulkDeletion extends Component
{
    use WithCachedRows;
    
    public $showUserBulkDeletionModal = false;

    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 render()
    {
        return view('livewire.backend.management.audience-management.logical-component.user-bulk-deletion');
    }
}

Context

  • Livewire version: 2.3.6
  • Laravel version: 8.23.1
  • Alpine version: 2.8.0
  • Browser: Chrome

来源:https://stackoverflow.com/questions/65798577/make-a-separate-livewire-component-to-delete-an-array-of-users-along-with-traits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!