public function store(PostFormRequest $request) { $post = new Post(['title' => $request->get('title'), 'content' => $request->get('content'), 'slug' => Str::slug($request->get('title'), '-')]); $post->save(); $post->categories()->sync($request->get('categories')); return redirect('/admin/posts/create')->with('status', 'The post has been created!'); }
/** * Remove given category from given post. * * @param Post $post * @param Category $category * @return void * @throws NotFoundHttpException */ public function removeCategory(Post $post, Category $category) { if (!$post->categories->contains($category->id)) { throw new NotFoundHttpException("Category doesn't exist on Post #" . $post->id); } $post->categories()->detach($category->id); }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { //handel the request $validator = Validator::make($request->all(), ['title' => 'required|unique:posts|max:67', 'image' => 'required|image|mimes:jpeg,png|max:5000']); if ($validator->fails()) { return redirect('seodashboard/posts/create')->withErrors($validator)->withInput(); } $post = new Post(); $post->title = $request->input('title'); $post->content = $request->input('content'); $post->published = $request->input('status'); $post->excrypt = str_limit($request->input('excrypt'), 155); $tags = $request->input('tags'); $categories = $request->input('categories'); $tags = $this->ConvertTagsToList($tags); //save image path in database if ($request->hasFile('image')) { $destinationPath = 'images'; $file = $request->file('image'); $fileName = $file->getClientOriginalName(); $file->move($destinationPath, $fileName); $post->image = $fileName; } $post->save(); $post->tags()->attach($tags); $post->categories()->attach($categories); return redirect('seodashboard/posts'); }