public function showPost($slug, Request $request) { $post = Post::with('tags')->whereSlug($slug)->firstOrFail(); $tag = $request->get('tag'); if ($tag) { $tag = Tag::whereTag($tag)->firstOrFail(); } return view($post->layout, compact('post', 'tag')); }
/** * Create a post. * * @param array $inputs * @param int $user_id * @return void */ public function store($inputs, $user_id) { $post = $this->savePost(new $this->model(), $inputs, $user_id); // Tags gestion if (array_key_exists('tags', $inputs) && $inputs['tags'] != '') { $tags = explode(',', $inputs['tags']); foreach ($tags as $tag) { $tag_ref = $this->tag->whereTag($tag)->first(); if (is_null($tag_ref)) { $tag_ref = new $this->tag(); $tag_ref->tag = $tag; $post->tags()->save($tag_ref); } else { $post->tags()->attach($tag_ref->id); } } } // Maybe purge orphan tags... }
/** * Update a post. * * @param array $inputs * @param App\Models\Post $post * @return void */ public function update($inputs, $post) { $post = $this->savePost($post, $inputs); // Tag control $tags_id = []; if (array_key_exists('tags', $inputs) && $inputs['tags'] != '') { $tags = explode(',', $inputs['tags']); foreach ($tags as $tag) { $tag_ref = $this->tag->whereTag($tag)->first(); if (is_null($tag_ref)) { $tag_ref = new $this->tag(); $tag_ref->tag = $tag; $tag_ref->save(); } array_push($tags_id, $tag_ref->id); } } $post->tags()->sync($tags_id); }