Files
muninn/app/Livewire/Sites/Index.php
T

73 lines
1.6 KiB
PHP
Raw Normal View History

2026-07-18 14:57:47 +03:00
<?php
namespace App\Livewire\Sites;
use App\Models\Site;
use Livewire\Attributes\Layout;
use Livewire\Component;
#[Layout('components.layouts.app')]
class Index extends Component
{
public bool $showForm = false;
public ?int $editingId = null;
public string $name = '';
public string $url = '';
public ?string $description = null;
public function render()
{
return view('livewire.sites.index', [
'sites' => Site::orderBy('name')->get(),
]);
}
public function create(): void
{
$this->resetForm();
$this->showForm = true;
}
public function edit(int $id): void
{
$site = Site::findOrFail($id);
$this->editingId = $site->id;
$this->name = $site->name;
$this->url = $site->url;
$this->description = $site->description;
$this->showForm = true;
}
public function save(): void
{
$validated = $this->validate([
'name' => ['required', 'string', 'max:255'],
'url' => ['required', 'string', 'max:255'],
'description' => ['nullable', 'string'],
]);
Site::updateOrCreate(['id' => $this->editingId], $validated);
$this->resetForm();
$this->showForm = false;
}
public function delete(int $id): void
{
Site::findOrFail($id)->delete();
}
public function resetForm(): void
{
$this->editingId = null;
$this->name = '';
$this->url = '';
$this->description = null;
$this->resetValidation();
}
}