LAR_Maintenance/app/Livewire/MaintenanceTask/Form.php

61 lines
1.4 KiB
PHP
Raw Normal View History

2024-08-06 06:31:51 +00:00
<?php
namespace App\Livewire\MaintenanceTask;
use Livewire\Component;
use App\Models\MaintenanceTask;
class Form extends Component
{
public $model;
public string $maintenance_id;
public string $name;
public string $description = "";
public $action = 'store';
protected function rules()
{
return [
'maintenance_id' => 'required',
'name' => 'required',
'description' => 'required',
];
}
public function mount ($model = null){
if (!empty($model)) {
$maintenanceTask = MaintenanceTask::find($model);
$this->model = $model;
$this->maintenance_id = $maintenanceTask->maintenance_id;
$this->name = $maintenanceTask->name;
$this->description = $maintenanceTask->description;
$this->action = 'update';
}
}
public function store()
{
$validatedData = $this->validate();
MaintenanceTask::create($validatedData);
$this->dispatch('closeModal');
}
public function update()
{
$validatedData = $this->validate();
$maintenanceTask = MaintenanceTask::find($this->model);
if (!empty($maintenanceTask)) {
$maintenanceTask->update($validatedData);
}
$this->dispatch('closeModal');
}
public function render()
{
return view('livewire.maintenance-task.form');
}
}