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