'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"); } } }