'deleteProvince' ]; protected $rules = [ 'name' => 'required' ]; public function resetFields(){ $this->name = ''; $this->enabled = true; } public function render() { $this->provinces = Province::select('id', 'name', 'enabled')->get(); return view('livewire.province'); } public function addProvince() { $this->resetFields(); $this->addProvince = true; $this->updateProvince = false; } public function storeProvince() { $this->validate(); try { Province::create([ 'name' => $this->name, 'nation_id' => $this->nationId, 'enabled' => $this->enabled ]); session()->flash('success','Provincia creata'); $this->resetFields(); $this->addProvince = false; } catch (\Exception $ex) { session()->flash('error','Errore in fase di salvataggio'); } } public function editProvince($id){ try { $province = Province::findOrFail($id); if( !$province) { session()->flash('error','Provincia non trovata'); } else { $this->name = $province->name; $this->enabled = $province->enabled; $this->nationId = $province->nationId; $this->provinceId = $province->id; $this->updateProvince = true; $this->addProvince = false; } } catch (\Exception $ex) { session()->flash('error','Errore'); } } public function updateProvince() { $this->validate(); try { Province::whereId($this->provinceId)->update([ 'name' => $this->name, 'nation_id' => $this->nationId, 'enabled' => $this->enabled ]); session()->flash('success','Provincia aggiornata'); $this->resetFields(); $this->updateProvince = false; } catch (\Exception $ex) { session()->flash('success','Errore'); } } public function cancelProvince() { $this->addProvince = false; $this->updateProvince = false; $this->resetFields(); } public function deleteProvince($id) { try{ Province::find($id)->delete(); session()->flash('success',"Provincia eliminata"); }catch(\Exception $e){ session()->flash('error',"Errore"); } } }