$post->title, "text" => $post->text, "id" => $post->id]; $record['commentCount'] = $post->comments()->count(); $record['tags'] = $post->tags; $result[] = $record; } return view('post_index', ['posts' => $result]); } public function create(){ return view('form', ['allTags' => Tag::all(), 'postTags' => []]); } public function edit($id){ $post = Post::find($id); $postTags = []; foreach($post->tags as $tag){ $postTags[] = $tag->id; } return view('form',['post' => $post, 'allTags' => Tag::all(), 'postTags' => $postTags]); } public function store(Request $req){ $newRecord = new Post([ 'title' =>Input::get('title'), 'text' =>Input::get('text')]); $newRecord->save(); return redirect(route('cntrl.index')); } public function update($id){ $post = Post::find($id); $post->title = Input::get('title'); $post->text = Input::get('text'); $tagIds = Input::get('tags'); $post->tags()->detach(); $post->tags()->attach($tagIds); $post->save(); return redirect(route('cntrl.index')); } public function destroy($id){ Post::destroy($id); return redirect(route('cntrl.index')); } public function show($id){ $post = Post::find($id); return view('post_show', ['post' => $post, "comments" => $post->comments()->orderBy('id')->get()]); } }