/** * Create a new task. * * @param Request $request * @return Response */ public function store(Request $request) { $this->authorize('blog', Auth::user()); $this->validate($request, Post::storeRules()); $this->validate($request, Category::storeRules()); $this->validate($request, Tag::storeRules()); \DB::transaction(function () use($request) { $post = Post::create(Post::storeAttributes($request)); $categories = explode(',', $request->categories); // remove empty items from array $categories = array_filter($categories); // trim all the items in array $categories = array_map('trim', $categories); $post->saveCategories($categories); $tags = explode(',', $request->tags); // remove empty items from array $tags = array_filter($tags); // trim all the items in array $tags = array_map('trim', $tags); $post->saveTags($tags); }); return redirect(route('blog')); }
/** * Update the given task. * * @param Request $request * @param Task $task * @return Response */ public function update(Request $request, Post $post) { $this->authorize('blog', Auth::user()); $this->validate($request, Post::storeRules()); $this->validate($request, Category::storeRules()); $this->validate($request, Tag::storeRules()); \DB::transaction(function () use($request, $post) { $post->update(Post::storeAttributes($request)); $categories = explode(',', $request->categories); // remove empty items from array $categories = array_filter($categories); // trim all the items in array $categories = array_map('trim', $categories); $post->saveCategories($categories); $tags = explode(',', $request->tags); // remove empty items from array $tags = array_filter($tags); // trim all the items in array $tags = array_map('trim', $tags); //\Debugbar::info($tags); $post->saveTags($tags); }); return redirect(route('post.edit', $post))->with('success', 'Profile updated!'); }
public function saveTags($tags) { $save_etis = []; foreach ($tags as $eti) { $eti = trim($eti); $eti = html_entity_decode($eti); $tmp = Tag::where('name', 'like', $eti)->get()->first(); if (is_null($tmp)) { $tmp = new Tag(['name' => $eti]); $tmp->save(); } array_push($save_etis, $tmp->id); } // \Debugbar::info($save_etis); if (count($save_etis) > 0) { $this->tags()->sync($save_etis); } }