81 lines
1.4 KiB
PHP
81 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Livewire\Form;
|
|
|
|
use App\Models\Card;
|
|
use App\Models\Character;
|
|
use App\Models\Raid;
|
|
use App\Models\Wow;
|
|
|
|
use App\Http\Livewire\Traits\Alert;
|
|
|
|
use Livewire\Component;
|
|
|
|
class CharacterForm extends Component
|
|
{
|
|
use Alert;
|
|
|
|
/**
|
|
* The character record
|
|
*/
|
|
public $character;
|
|
|
|
/**
|
|
* True if the record already exists in the database.
|
|
*/
|
|
public bool $exist;
|
|
|
|
public function mount(Character $character)
|
|
{
|
|
$this->character = $character;
|
|
$this->exist = $character->exists;
|
|
}
|
|
|
|
/**
|
|
* Validation rules
|
|
*/
|
|
protected function rules()
|
|
{
|
|
return [
|
|
'character.name' => 'required|string|min:3|max:14',
|
|
];
|
|
}
|
|
|
|
public function updated($property, $value)
|
|
{
|
|
$this->validateOnly($property);
|
|
}
|
|
|
|
/**
|
|
* Returns true if this card has not been stored in the database.
|
|
*/
|
|
public function isNew() : bool
|
|
{
|
|
return !$this->exist;
|
|
}
|
|
|
|
/**
|
|
* Submit the form, create/update card.
|
|
*/
|
|
public function submit()
|
|
{
|
|
$this->validate();
|
|
$this->character->save();
|
|
|
|
if ($this->isNew()) {
|
|
session()->flash('info', 'Character was successfully created.');
|
|
return redirect()->route('admin.character.index');
|
|
}
|
|
|
|
$this->info('Character was successfully updated.');
|
|
}
|
|
|
|
/**
|
|
* Render the setup page
|
|
*/
|
|
public function render()
|
|
{
|
|
return view('form.character')
|
|
->layout('layouts.admin');
|
|
}
|
|
}
|