@@ -65,14 +65,17 @@ public function store(Request $request, PostRepository $postRepository)
6565 'meta_description ' => 'nullable|string ' ,
6666 'image ' => 'nullable|image ' ,
6767 'category_ids ' => 'required|array ' ,
68- 'category_ids.* ' => 'exists:post_categories,id '
68+ 'category_ids.* ' => 'exists:post_categories,id ' ,
69+ 'tag_ids ' => 'sometimes|required|array ' ,
70+ 'tag_ids.* ' => 'required|integer|exists:tags,id '
6971 ]);
7072
7173 $ request ->merge (['user_id ' => auth ()->user ()->id ]);
7274
7375 $ post = Post::create ($ request ->all ());
7476 $ postRepository ->uploadImage ($ post , $ request , 'image ' );
7577 $ post ->categories ()->sync ($ request ->get ('category_ids ' ));
78+ $ post ->tags ()->sync ($ request ->get ('tag_ids ' ));
7679
7780 return redirect ()->route ('admin.posts.edit ' , $ post ->id )->with (['flash_type ' => 'success ' , 'flash_message ' => 'Post created successfully ' , 'flash_description ' => $ post ->title ]);
7881 }
@@ -88,13 +91,16 @@ public function update(Request $request, PostRepository $postRepository, $id)
8891 'meta_description ' => 'nullable|string ' ,
8992 'image ' => 'nullable|image ' ,
9093 'category_ids ' => 'required|array ' ,
91- 'category_ids.* ' => 'exists:post_categories,id '
94+ 'category_ids.* ' => 'exists:post_categories,id ' ,
95+ 'tag_ids ' => 'sometimes|required|array ' ,
96+ 'tag_ids.* ' => 'required|integer|exists:tags,id '
9297 ]);
9398 $ post = Post::findOrFail ($ id );
9499 $ post ->fill ($ request ->all ());
95100 $ post ->save ();
96101 $ postRepository ->uploadImage ($ post , $ request , 'image ' );
97102 $ post ->categories ()->sync ($ request ->get ('category_ids ' ));
103+ $ post ->tags ()->sync ($ request ->get ('tag_ids ' ));
98104 return redirect ()->route ('admin.posts.edit ' , $ id )->with (['flash_type ' => 'success ' , 'flash_message ' => 'Post updated successfully ' , 'flash_description ' => $ post ->title ]);
99105 }
100106}
0 commit comments