LAR_Maintenance/app/Livewire/MaintenanceHistory/Form.php

61 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2024-07-31 15:04:33 +00:00
<?php
namespace App\Livewire\MaintenanceHistory;
use Livewire\Component;
use App\Models\MaintenanceHistory;
class Form extends Component
{
public $model;
public string $maintenance_id;
public string $start_at;
public string $finished_at;
public $action = 'store';
protected function rules()
{
return [
'maintenance_id' => 'required',
'start_at' => 'required',
'finished_at' => 'required',
];
}
public function mount ($model = null){
if (!empty($model)) {
2024-08-16 11:28:14 +00:00
$maintenanceHistory = MaintenanceHistory::find($model);
2024-07-31 15:04:33 +00:00
$this->model = $model;
2024-08-16 11:28:14 +00:00
$this->maintenance_id = $maintenanceHistory->maintenance_id;
$this->start_at = $maintenanceHistory->start_at;
$this->finished_at = $maintenanceHistory->finished_at;
2024-07-31 15:04:33 +00:00
$this->action = 'update';
}
}
public function store()
{
$validatedData = $this->validate();
MaintenanceHistory::create($validatedData);
$this->dispatch('closeModal');
}
public function update()
{
$validatedData = $this->validate();
2024-08-16 11:28:14 +00:00
$maintenanceHistory = MaintenanceHistory::find($this->model);
if (!empty($maintenanceHistory)) {
$maintenanceHistory->update($validatedData);
2024-07-31 15:04:33 +00:00
}
$this->dispatch('closeModal');
}
public function render()
{
return view('livewire.components.maintenance-history.form');
}
}