Last active
October 21, 2020 10:49
-
-
Save raselupm/d8e98aaada50bf0557b42fc8c8ca1250 to your computer and use it in GitHub Desktop.
edit-state.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Livewire; | |
use App\Models\State; | |
use Livewire\Component; | |
class EditState extends Component | |
{ | |
public $name; | |
public $code; | |
public $state_id; | |
public function mount($stateID) | |
{ | |
$state = State::findOrFail($stateID); | |
$this->name = $state->name; | |
$this->code = $state->code; | |
$this->state_id = $stateID; | |
return view('livewire.edit-state' ); | |
} | |
protected $rules = [ | |
'code' => 'required|max:2' | |
]; | |
public function updated($propertyName) { | |
$this->validateOnly( | |
$propertyName, | |
array_merge( | |
$this->rules, | |
[ | |
'name' => 'required|unique:states,name,' . $this->state_id, | |
'code' => 'required|max:2|unique:states,code,' . $this->state_id | |
] | |
) | |
); | |
} | |
public function submitForm() { | |
$this->validate( | |
array_merge( | |
$this->rules, | |
[ | |
'name' => 'required|unique:states,name,' . $this->state_id, | |
'code' => 'required|max:2|unique:states,code,' . $this->state_id | |
] | |
) | |
); | |
$state = State::findOrFail($this->state_id); | |
$state->name = $this->name; | |
$state->code = $this->code; | |
$state->save(); | |
return \redirect()->to('/states'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment