/** * @param Post $post * @return \Illuminate\View\View */ public function show(Post $post) { $teacher = Teacher::find($post->author_id); $category_id = CategoryPost::wherePost_id($post->id)->first()->category_id; $category = Category::find($category_id)->name; $post['category_id'] = $category_id; $post['category'] = $category; $post['author'] = $teacher->name; $post['fb'] = $teacher->fb; if (!Auth::user()) { $post->increment('views'); } return $post; }
/** * Display the specified resource. * * @param int $id * @return Response */ public function show($id) { $posts = Post::whereArchive_id($id)->paginate(Input::get('itemPerPage')); for ($i = 0; $i < count($posts); $i++) { $post = $posts[$i]; $post = array_except($post, array("body")); $teacher = Teacher::find($post->author_id); $category_id = CategoryPost::wherePost_id($post->id)->first()->category_id; $category = Category::find($category_id)->name; $post['category'] = $category; $post['author'] = $teacher->name; $post['fb'] = $teacher->fb; $posts[$i] = $post; } return $posts; }
/** * Save List of Categories to category_post table * @param Post $model */ private function saveCategoriesList(Post $model) { // $model->unlinkAll('categories', true); $categoryPosts = $model->categoryPosts; foreach ($categoryPosts as $category) { $category->delete(); } foreach ($model->categoriesList as $categoryId) { $categoryPost = new CategoryPost(); $categoryPost->post_id = $model->id; $categoryPost->category_id = $categoryId; $categoryPost->save(); } }
/** * @return \yii\db\ActiveQuery */ public function getPosts() { return $this->hasMany(Post::className(), ['id' => 'post_id'])->viaTable(CategoryPost::tableName(), ['category_id' => 'id']); }
/** * @return \yii\db\ActiveQuery */ public function getCategoryPosts() { return $this->hasMany(CategoryPost::className(), ['post_id' => 'id']); }