When I want to edit the post with the post's existing tags it only shows one tag though there are more tags in the selected post. I also want to input new tags in the post-editing blade.
Post creating code
//Store Post public function storeNewPost(Request $request){ //return $request->all(); $request->validate([ 'post_title' => 'required', 'post_details' => 'required', 'category_id' => 'required', 'image' => 'image|max:15360|dimensions:max_width=4000,max_height=3000' ]); $image = $request->file('post_thumbnail'); $name_gen=uniqid().'.'.$image->getClientOriginalExtension(); Image::make($image)->resize(700,400)->save('frontend/assets/images/post/'.$name_gen); $save_url = 'frontend/assets/images/post/'.$name_gen; $post = Post::create([ 'user_id' => Auth::id(), 'post_uper_title' =>$request->post_uper_title, 'post_title' =>$request->post_title, 'post_sub_title' =>$request->post_sub_title, 'post_details' =>$request->post_details, 'post_slug' =>str_replace(' ', '-', $request->post_title), 'seo_title' =>$request->seo_title, 'seo_descp' =>$request->seo_descp, 'lead' =>$request->lead, 'lead2' =>$request->lead2, 'featured' =>$request->featured, 'repoter_name' =>$request->repoter_name, 'division_id' =>$request->division_id, 'district_id' =>$request->district_id, 'category_id' =>$request->category_id, 'post_thumbnail' =>$save_url, 'thumbnail_caption' =>$request->thumbnail_caption, 'thumbnail_alt' =>$request->thumbnail_alt, 'created_at' => Carbon::now(), ]); if($post){ $tags = explode(",", implode($request->tags)); $tagNames = []; if (!empty($tags)) { foreach ($tags as $tagName) { $tag = Tags::firstOrCreate(['name'=>$tagName]); if($tag) { $tagNames[] = $tag->id; } } } $post->tags()->sync($tagNames); $notification = array( 'message' => 'Post Inserted Successfully', 'alert-type' => 'success' ); return redirect()->route('all.posts')->with($notification); }else{ return back(); } }//end insert post
** post update code in post controller **
//Edit Post public function editPost($news_id){ $editPost = Post::findOrFail($news_id); $postDivisions = Division::get(); $postCats = Category::get(); $post_dist = District::get(); $post_tags = Tags::all(); return view('admin.post.edit-post', compact('postDivisions', 'postCats', 'editPost', 'post_dist', 'post_tags')); } //Update Post public function updatePost(Request $request){ $news_id = $request->id; $image = $request->file('post_thumbnail'); $oldimage = $request->oldimage; if ($image) { $name_gen=hexdec(uniqid()).'.'.$image->getClientOriginalExtension(); Image::make($image)->resize(700,400)->save('frontend/assets/images/post/'.$name_gen); $save_url = 'frontend/assets/images/post/'.$name_gen; if($oldimage){ unlink($oldimage); } Post::findOrFail($news_id)->update([ 'user_id' => Auth::id(), 'post_uper_title' =>$request->post_uper_title, 'post_title' =>$request->post_title, 'post_sub_title' =>$request->post_sub_title, 'post_details' =>$request->post_details, 'post_tags' =>$request->post_tags, 'post_slug' =>$request->post_slug, 'seo_title' =>$request->seo_title, 'seo_descp' =>$request->seo_descp, 'lead' =>$request->lead, 'lead2' =>$request->lead2, 'featured' =>$request->featured, 'repoter_name' =>$request->repoter_name, 'division_id' =>$request->division_id, 'district_id' =>$request->district_id, 'category_id' =>$request->category_id, 'post_thumbnail' =>$save_url, 'thumbnail_caption' =>$request->thumbnail_caption, 'thumbnail_alt' =>$request->thumbnail_alt, 'created_at' => Carbon::now(), ]); $notification = array( 'message' => 'Post Updated Successfully', 'alert-type' => 'success' ); return redirect()->route('all.posts')->with($notification); }else{ Post::findOrFail($news_id)->update([ 'user_id' => Auth::id(), 'post_uper_title' =>$request->post_uper_title, 'post_title' =>$request->post_title, 'post_sub_title' =>$request->post_sub_title, 'post_details' =>$request->post_details, 'post_tags' =>$request->post_tags, 'post_slug' =>$request->post_slug, 'seo_title' =>$request->seo_title, 'seo_descp' =>$request->seo_descp, 'lead' =>$request->lead, 'lead2' =>$request->lead2, 'featured' =>$request->featured, 'repoter_name' =>$request->repoter_name, 'division_id' =>$request->division_id, 'district_id' =>$request->district_id, 'category_id' =>$request->category_id, 'post_thumbnail' =>$oldimage, 'thumbnail_caption' =>$request->thumbnail_caption, 'thumbnail_alt' =>$request->thumbnail_alt, 'created_at' => Carbon::now(), ]); $notification = array( 'message' => 'Post Updated Successfully', 'alert-type' => 'success' ); return redirect()->route('all.posts')->with($notification); } }//end update post
** post editing blade's tags code **
<div class="form-group">
<label class="form-control-label">{{__('Post Tags')}}</label>
<input type="text" class="w-100" id="tagnames" name="tags[]" data-role="tagsinput"
@foreach ($editPost->tags as $tags) value="{{$tags->name}}" @endforeach>
</div>
CodePudding user response:
If you have manyToMany relation in your models use sync()
method on update method.
Example:
$post->tags()->sync($request->tags);
CodePudding user response:
// try this
<input type="text" class="w-100" id="tagnames" name="tags[]" data-role="tagsinput" value="@foreach($editPost->tags as $tags){{$tags->name.','}}@endforeach">