'required' ]; protected $messages = [ 'name.required' => 'Il nome รจ obbligatorio' ]; public function resetFields(){ $this->name = ''; $this->enabled = true; } public function render() { $this->categories = Category::where('parent_id', null)->get(); return view('livewire.category'); } public function addCategory() { $this->resetFields(); $this->addCategory = true; $this->updateCategory = false; } public function storeCategory() { $this->validate(); try { Category::create([ 'name' => $this->name, 'parent' => $this->parent_id, 'enabled' => $this->enabled ]); session()->flash('success','Categoria creata'); $this->resetFields(); $this->addCategory = false; } catch (\Exception $ex) { session()->flash('error','Errore in fase di salvataggio'); } } public function editCategory($id){ try { $category = Category::findOrFail($id); if( !$category) { session()->flash('error','Categoria non trovata'); } else { $this->name = $category->name; $this->enabled = $category->enabled; $this->parent_id = $category->parent_id; $this->dataId = $category->id; $this->updateCategory = true; $this->addCategory = false; } } catch (\Exception $ex) { session()->flash('error','Errore'); } } public function updateCategory() { $this->validate(); try { Category::whereId($this->dataId)->update([ 'name' => $this->name, 'parent' => $this->parent_id, 'enabled' => $this->enabled ]); session()->flash('success','Categoria aggiornata'); $this->resetFields(); $this->updateCategory = false; } catch (\Exception $ex) { session()->flash('success','Errore'); } } public function cancelCategory() { $this->addCategory = false; $this->updateCategory = false; $this->resetFields(); } public function deleteCategory($id) { try{ Category::find($id)->delete(); session()->flash('success',"Categoria eliminata"); }catch(\Exception $e){ session()->flash('error',"Errore"); } } }