LAR_Maintenance/app/Livewire/MaintenanceHistory/Form.php

61 lines
1.4 KiB
PHP

<?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)) {
$maintenanceHistory = MaintenanceHistory::find($model);
$this->model = $model;
$this->maintenance_id = $maintenanceHistory->maintenance_id;
$this->start_at = $maintenanceHistory->start_at;
$this->finished_at = $maintenanceHistory->finished_at;
$this->action = 'update';
}
}
public function store()
{
$validatedData = $this->validate();
MaintenanceHistory::create($validatedData);
$this->dispatch('closeModal');
}
public function update()
{
$validatedData = $this->validate();
$maintenanceHistory = MaintenanceHistory::find($this->model);
if (!empty($maintenanceHistory)) {
$maintenanceHistory->update($validatedData);
}
$this->dispatch('closeModal');
}
public function render()
{
return view('livewire.components.maintenance-history.form');
}
}