| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- class Nation extends Component
- {
- public $nations, $name, $enabled, $nationId, $updateNation = false, $addNation = false;
- protected $listeners = [
- 'deleteNationListner'=>'deleteNation'
- ];
- protected $rules = [
- 'name' => 'required'
- ];
- public function resetFields(){
- $this->name = '';
- $this->enabled = true;
- }
- public function render()
- {
- $this->nations = Nation::select('id', 'name', 'enabled')->get();
- return view('livewire.nation');
- }
- public function addNation()
- {
- $this->resetFields();
- $this->addNation = true;
- $this->updateNation = false;
- }
- public function storeNation()
- {
- $this->validate();
- try {
- Nation::create([
- 'name' => $this->name,
- 'enabled' => $this->enabled
- ]);
- session()->flash('success','Nazione creata');
- $this->resetFields();
- $this->addNation = false;
- } catch (\Exception $ex) {
- session()->flash('error','Errore in fase di salvataggio');
- }
- }
- public function editNation($id){
- try {
- $nation = Nation::findOrFail($id);
- if( !$nation) {
- session()->flash('error','Nazione non trovata');
- } else {
- $this->name = $nation->name;
- $this->enabled = $nation->enabled;
- $this->nationId = $nation->id;
- $this->updateNation = true;
- $this->addNation = false;
- }
- } catch (\Exception $ex) {
- session()->flash('error','Errore');
- }
- }
- public function updateNation()
- {
- $this->validate();
- try {
- Nation::whereId($this->nationId)->update([
- 'name' => $this->name,
- 'enabled' => $this->enabled
- ]);
- session()->flash('success','Nazione aggiornata');
- $this->resetFields();
- $this->updateNation = false;
- } catch (\Exception $ex) {
- session()->flash('success','Errore');
- }
- }
- public function cancelNation()
- {
- $this->addNation = false;
- $this->updateNation = false;
- $this->resetFields();
- }
- public function deleteNation($id)
- {
- try{
- Nation::find($id)->delete();
- session()->flash('success',"Nazione eliminata");
- }catch(\Exception $e){
- session()->flash('error',"Errore");
- }
- }
- }
|