'required' ]; protected $messages = [ 'name.required' => 'Il nome รจ obbligatorio' ]; public function resetFields(){ $this->name = ''; $this->fiscal_code = ''; $this->vat = ''; $this->address = ''; $this->zip_code = ''; $this->nation_id = null; $this->province_id = null; $this->city_id = null; $this->referent = ''; $this->website = ''; $this->phone = ''; $this->email = ''; $this->enabled = true; } public function mount() { if (isset($_GET["new"])) $this->add(); $this->nations = \App\Models\Nation::select('id', 'name')->get(); $this->provinces = array(); $this->cities = array(); } public function loadProvinces() { $this->provinces = \App\Models\Province::select('id', 'name')->where('nation_id', $this->nation_id)->get(); $this->cities = array();\App\Models\City::select('id', 'name')->get(); } public function loadCities() { $this->cities = \App\Models\City::select('id', 'name')->where('province_id', $this->province_id)->get(); } public function render() { $this->records = \App\Models\Supplier::with('nation')->get(); return view('livewire.supplier'); } public function add() { $this->resetFields(); $this->add = true; $this->update = false; } public function store() { $this->validate(); try { \App\Models\Supplier::create([ 'name' => $this->name, 'fiscal_code' => $this->fiscal_code, 'vat' => $this->vat, 'address' => $this->address, 'zip_code' => $this->zip_code, 'nation_id' => $this->nation_id, 'province_id' => $this->province_id, 'city_id' => $this->city_id, 'referent' => $this->referent, 'website' => $this->website, 'phone' => $this->phone, 'email' => $this->email, 'enabled' => $this->enabled ]); session()->flash('success','Fornitore creato'); $this->resetFields(); $this->add = false; } catch (\Exception $ex) { session()->flash('error','Errore in fase di salvataggio'); } } public function edit($id){ try { $supplier = \App\Models\Supplier::findOrFail($id); if( !$supplier) { session()->flash('error','Fornitore non trovato'); } else { $this->name = $supplier->name; $this->fiscal_code = $supplier->fiscal_code; $this->vat = $supplier->vat; $this->address = $supplier->address; $this->zip_code = $supplier->zip_code; $this->nation_id = $supplier->nation_id; $this->province_id = $supplier->province_id; $this->city_id = $supplier->city_id; $this->referent = $supplier->referent; $this->website = $supplier->website; $this->phone = $supplier->phone; $this->email = $supplier->email; $this->dataId = $supplier->id; $this->update = true; $this->add = false; } } catch (\Exception $ex) { session()->flash('error','Errore'); } } public function update() { $this->validate(); try { \App\Models\Supplier::whereId($this->dataId)->update([ 'name' => $this->name, 'fiscal_code' => $this->fiscal_code, 'vat' => $this->vat, 'address' => $this->address, 'zip_code' => $this->zip_code, 'nation_id' => $this->nation_id, 'province_id' => $this->province_id, 'city_id' => $this->city_id, 'referent' => $this->referent, 'website' => $this->website, 'phone' => $this->phone, 'email' => $this->email, 'enabled' => $this->enabled ]); session()->flash('success','Fornitore aggiornato'); $this->resetFields(); $this->update = false; } catch (\Exception $ex) { session()->flash('success','Errore'); } } public function cancel() { $this->add = false; $this->update = false; $this->resetFields(); } public function delete($id) { try{ \App\Models\Supplier::find($id)->delete(); session()->flash('success',"Fornitore eliminato"); }catch(\Exception $e){ session()->flash('error',"Errore"); } } }