当前位置: 首页 > 知识库问答 >
问题:

如何在laravel 8中使用旧标签和新标签更新帖子?

柴博
2023-03-14

当我想用帖子的现有标签编辑帖子时,它只显示一个标签,尽管所选帖子中有更多标签。我还想在后期编辑刀片中输入新标签。

后期创建代码


           //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控制器中的post更新代码**


        //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

**后期编辑刀片的标签代码**

         <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>

共有2个答案

吴高畅
2023-03-14
 // try this 
 <input type="text" class="w-100" id="tagnames" name="tags[]" data-role="tagsinput"  value="@foreach($editPost->tags as $tags){{$tags->name.','}}@endforeach">
江洲
2023-03-14

如果模型中有多个关系,请使用更新方法上的sync()方法。

示例:$post-

 类似资料:
  • 问题内容: 我有一个活动,该活动的TabHost包含一组TabSpec,每个TabSpec都有一个listview,其中包含要由该选项卡显示的项目。创建每个TabSpec时,我设置一个图标以显示在选项卡标题中。 TabSpec是通过以下方法创建的,该方法循环创建适当数量的选项卡: 有几个实例,我希望能够更改程序执行过程中每个选项卡中显示的图标。目前,我正在删除所有选项卡,并再次调用上述代码以重新创

  • 我目前正在实现一个搜索功能来从wordpress搜索帖子 我想通过标签、标题来搜索文章,并以JSON的形式返回 我发现了一个将结果生成为JSON(JSON API)的插件 http://wordpress.org/plugins/json-api/other_notes/#2.1.-Core-controller-methods 在该插件中,有一个get_posts函数,可以返回支持WP_quer

  • 问题内容: 此代码示例属于Stopwatch类的一部分,该类属于一个较大的项目的一部分,该更大的项目的目的是成为模仿Android Clock的桌面gui应用。我具有秒,分钟,小时等标签,这些标签应该从计时器任务内部的无限while循环中更新,而该计时器任务在布尔状态为true时运行。while循环应该实时更新GUI标签。我让计时器任务每毫秒执行一次。为什么我的GUI在程序更新第一个Label后立

  • 问题内容: 我在使用Tkinter和PIL更新标签时遇到了一个“小”问题。 当我按下起 司 按钮时,它应该显示foo2,但是它只显示白色屏幕。有什么建议? 这是我的代码: 问题答案: 好吧,我认为原因是当您按下按钮时。这是因为此行: 应该: 出现这种情况,是因为,等回报。

  • 我有一个标签,这是使用了多次在我的网页。它工作得很好,直到我尝试按Ajax更新标签。结果:只有第一个标签得到更新。这是一个已知的问题吗?我不确定,因为我不能打开JIRA页面:https://issues.apache.org/JIRA/browse/wicket(一直在跳)。我使用的是wicket 7.3.0版本 =>只更新第一个标签。我有一个解决办法,通过添加4个不同的标签实例与相同的文本内容。

  • 这是我写的代码,以打开一个新标签在已经打开Chrome但它是重定向到第二个url在现有的标签只有。 我想打开一个新选项卡并加载“www.mailinator.com”的url 有人能帮我吗?