LAR_Maintenance/app/Livewire/Host/Form.php

64 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2024-07-30 16:13:21 +00:00
<?php
namespace App\Livewire\Host;
use Livewire\Component;
use App\Models\Host;
2024-09-25 20:09:13 +00:00
use App\Models\HostGroup;
2024-07-30 16:13:21 +00:00
class Form extends Component
{
public $model;
public string $hostname;
2024-09-25 20:09:13 +00:00
public $host_groups = [];
public $host_groups_available = [];
2024-07-30 16:13:21 +00:00
public $action = 'store';
protected function rules()
{
return [
'hostname' => 'required',
];
}
public function mount ($model = null){
2024-09-25 20:09:13 +00:00
$this->host_groups_available = HostGroup::pluck('name', 'id')->toArray();
2024-07-30 16:13:21 +00:00
if (!empty($model)) {
$host = Host::find($model);
$this->model = $model;
$this->hostname = $host->hostname;
2024-09-25 20:09:13 +00:00
$this->host_groups = $host->hostGroups()->pluck('host_groups.id')->toArray();
2024-07-30 16:13:21 +00:00
$this->action = 'update';
}
}
public function store()
{
$validatedData = $this->validate();
2024-09-25 20:09:13 +00:00
$host = Host::create($validatedData);
$host->hostGroups()->sync($this->host_groups);
2024-07-30 16:13:21 +00:00
$this->dispatch('closeModal');
}
public function update()
{
$validatedData = $this->validate();
$host = Host::find($this->model);
if (!empty($host)) {
$host->update($validatedData);
2024-09-25 20:09:13 +00:00
$host->hostGroups()->sync($this->host_groups);
2024-07-30 16:13:21 +00:00
}
$this->dispatch('closeModal');
}
public function render()
{
return view('livewire.host.form');
}
}