feat: add Sites Livewire CRUD page
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user