public function update(Requests\ProductRequest $request, $id) { $input = $request->all(); $tags_update = array_map('trim', explode(',', $input['tags'])); $array_update_tags = array(); foreach ($tags_update as $tag) { if (!empty($tag)) { if (!in_array($tag, Tag::lists('name')->toArray())) { Tag::create(['name' => $tag]); } array_push($array_update_tags, Tag::lists('id', 'name')->toArray()[$tag]); } } $this->productModel->find($id)->update($input); $this->productModel->find($id)->tags()->sync($array_update_tags); return redirect()->route('admin.products.index'); }
private function savetags($id, $tags) { // transforma objeto de csv para lista $tag_list = explode(',', $tags); $tag_list = array_map('trim', $tag_list); $tag_id_list = []; // atualiza tabela de tags foreach ($tag_list as $tag) { $tag = ltrim($tag); if ($tag != '') { $tag_reg = Tag::ofName($tag)->get(); if ($tag_reg->count() == 0) { $tag_reg = Tag::create(['name' => $tag]); $tag_id_list[] = $tag_reg->id; } else { $tag_id_list[] = $tag_reg[0]->id; } } } $product = $this->productsmodel->find($id); $product->tags()->sync($tag_id_list); }