nimuli/app/Livewire/Modals/DeleteQrCode.php

60 lines
1.5 KiB
PHP

<?php
namespace App\Livewire\Modals;
use App\Domains\QrCode\Models\QrCode;
use App\Domains\Workspace\Models\Workspace;
use Illuminate\View\View;
use LivewireUI\Modal\ModalComponent;
class DeleteQrCode extends ModalComponent
{
public int $workspaceId = 0;
public string $qrUlid = '';
public string $qrLabel = '';
public function mount(string $qrUlid): void
{
$qr = QrCode::where('ulid', $qrUlid)->firstOrFail();
$this->authorizeWorkspace($qr->workspace_id);
$this->workspaceId = $qr->workspace_id;
$this->qrUlid = $qrUlid;
$this->qrLabel = $qr->label ?? $qr->ulid;
}
public function delete(): void
{
QrCode::where('ulid', $this->qrUlid)
->where('workspace_id', $this->workspaceId)
->firstOrFail()
->delete();
$this->dispatch('qr-deleted')->to(\App\Livewire\Pages\QrCodes\Index::class);
$this->dispatch('toast', message: 'QR Code gelöscht', type: 'success');
$this->closeModal();
}
public static function modalMaxWidth(): string
{
return 'sm';
}
public function render(): View
{
return view('livewire.modals.delete-qr-code');
}
private function authorizeWorkspace(int $workspaceId): void
{
Workspace::where("id", $workspaceId)
->where(fn ($q) => $q
->where("owner_id", auth()->id())
->orWhereHas("members", fn ($q) => $q->where("user_id", auth()->id()))
)->firstOrFail();
}
}