/** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(Requests\ProductRequest $request) { $input = $request->all(); $tags = $request->only('tags'); $tagsId = $this->storeTags($tags); $product = $this->productModel->fill($input); $product->save(); $product->tags()->attach($tagsId); return redirect()->route('products'); }
private function updateProductTag(ProductRequest $request, $id) { $product = $this->productModel->find($id); $idsTagsToSync = array(); $tagsFromRequest = explode(',', $request->get('tags')); foreach ($tagsFromRequest as $tag) { $tagId = $this->tagModel->where('name', '=', $tag)->get(['id'])->first(); if (empty($tagId["id"])) { $tagId = $this->tagModel->create(['name' => $tag]); } $idsTagsToSync[] = $tagId["id"]; } $product->tags()->sync($idsTagsToSync); }