从用户中分离角色并批量删除用户以及 livewire 特征

Detach role from the user and delete the user in bulk along with livewire traits

描述

我在用户和角色之间有一个“belongsToMany”eloquent关系。我正在创建一个能够执行批量操作的 CRUD 系统。我关注了 Livewire 截屏视频 Bulk Export/Delete and Refactoring For Re-Usability。我已经为批量操作创建了一个特征,所以我可以开箱即用。

我需要将角色与用户分离,然后批量删除用户。我无法在 public 方法 属性 上调用 roles 关系并将其分离。这就是我为单个用户 $this->user->roles()->detach(); 分离角色的方式,但在批量删除用户的情况下,我无法使用 $this->selectedRowsQuery->roles()->detach(); 执行此操作。

精简、可复制粘贴的代码片段

public $showUserBulkDeletionModal = false;

public function confirmDeleteBulk()
{
    $deleteCount = $this->selectedRowsQuery->count();
        
    $this->selectedRowsQuery->roles()->detach();
    $this->selectedRowsQuery->delete();
    $this->showUserBulkDeletionModal = false;

    $this->notify('You\'ve deleted '.$deleteCount.' users');
}

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);
}
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));
    }
}

上下文

这条线行不通:

$this->selectedRowsQuery->roles()->detach();

$this->selectedRowsQuery 是一个集合,您的代码不够智能,无法知道您要分离(删除)roles() 的哪个实例。您只需要循环执行此操作:

foreach ($this->selectedRowsQuery as $queryRow) {
  $queryRow->roles()->detach();
  $queryRow->delete(); // Can do this here (single delete), or below
}

$this->selectedRowsQuery->delete(); // Can do this here (batch delete), or above

编辑:在 foreach() 时,$this->selectedRowsQuery 仍然是 Builder class 的实例,与 foreach() 不兼容,直到传递了闭包(get()cursor() 等)。要处理此问题,只需将您的代码调整为:

foreach ($this->selectedRowsQuery->get() as $queryRow) {
  $queryRow->roles()->detach();
  ...
}

注意:->get() 使用更广泛,但 ->cursor() 可用并且通常对于较大的循环性能更高。