LAR_Maintenance/app/Livewire/Host/Form.php

64 lines
1.4 KiB
PHP

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