LAR_Maintenance/app/Livewire/Task/Form.php

58 lines
1.1 KiB
PHP
Raw Permalink Normal View History

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