为啥当我编辑并保存对一个帖子的更改时,所有其他帖子都会进行相同的更改?我该如何解决?我正在使用 laravel 8

Posted

技术标签:

【中文标题】为啥当我编辑并保存对一个帖子的更改时,所有其他帖子都会进行相同的更改?我该如何解决?我正在使用 laravel 8【英文标题】:Why is it that when I edit and save changes to one post, all the other posts take on those same changes? How can I fix? I am using laravel 8为什么当我编辑并保存对一个帖子的更改时,所有其他帖子都会进行相同的更改?我该如何解决?我正在使用 laravel 8 【发布时间】:2021-06-11 15:30:59 【问题描述】:

每当我尝试仅将更改保存到一个帖子时,结果是我拥有的所有其他帖子都进行了相同的更改。

我在 web.php 中的路线是:

use App\Http\Controllers\PostsController;

Route::get('/posts/post/edit', [PostsController::class, 'edit']);

Route::patch('/posts/post', [PostsController::class, 'update'])->name('posts.update');

我在 Posts Controller 中的编辑功能是:

public function edit(Post $post)
  
    return view('posts.edit-post', ['post' => $post]);
  

我在 Posts Controller 中的更新功能是:

public function update(Post $post, Request $request)
  
    $data = request()->validate([
      'caption' => 'required',
      'url' => 'required',
      'image' => ['required', 'image'],
    ]);

    $imagePath = request('image')->store('uploads', 'public');

    auth()->user()->posts()->update([
      'caption' => $data['caption'],
      'url' => $data['url'],
      'image' => $imagePath,
    ]);

    return redirect('/users/' . auth()->user()->id);

  

我在edit-post.blade.php文件中的表单是:

<form action="('/posts/' . $post->id)" enctype="multipart/form-data" method="post">
      @csrf
      @method('PATCH')

      <div class="form-group">
          <label for="caption" class="create_caption_label">Post Caption</label>

          <div class="create_caption_div">
              <input id="caption"
              type="text"
              class="form-control @error('caption') is-invalid @enderror"
              name="caption"
              value=" old('caption') ?? $post->caption "
              autocomplete="caption" autofocus>

              @error('caption')
              <div class="invalid-feedback-div">
                <span class="invalid-feedback" role="alert">
                    <strong> $message </strong>
                </span>
              </div>
              @enderror
          </div>
      </div>

      <div class="form-group">
          <label for="url" class="edit_title_label">URL</label>

          <div class="edit_url_div">
              <input id="url"
              type="text"
              class="form-control @error('url') is-invalid @enderror"
              name="url"
              value=" old('url') ?? $post->url "
              autocomplete="url" autofocus>

              @error('url')
              <div class="invalid-feedback-div">
                <span class="invalid-feedback" role="alert">
                    <strong> $message </strong>
                </span>
              </div>
              @enderror
          </div>
      </div>

      <div class="create_post_image_div">
        <label for="image" class="create_image_label">Post Image</label>
        <input type="file" class="form-control-file" id="image" name="image">

        @error('image')
        <div class="invalid-feedback-div">
          <strong> $message </strong>
        </div>
        @enderror

        <div class="create_post_btn_div">
          <button class="create_post_btn">Save Changes</button>
        </div>
      </div>

    </form>

最后,这里是 profile.blade.php 文件,我想将帖子的数据输出到:

@foreach( $user->posts as $post )
              <div class="carousel_posts_container">

                <div class="post_date_and_edit_div">
             
                  <div class="post_edit_div">
                    <form action="/posts/$post->id/edit">
                      <input class="post_edit_btn" type="submit" value="• • •">
                    </form>
                  </div>
                </div>
             
                <div class="carousel_post_img_div">
                  <img src="/storage/ $post->image " class="carousel_img_placeholder">
                </div>                  

                <div class="carousel_caption_container">

                  <div class="carousel_caption_div">
                    <p class="carousel_caption_username">$user->username - $post->caption</p>
                  </div>

                </div>

              </div>
 @endforeach

【问题讨论】:

【参考方案1】:

在你的控制器中替换这个:

auth()->user()->posts()->update([
  'caption' => $data['caption'],
  'url' => $data['url'],
  'image' => $imagePath,
]);

用这个:

$post->update([
  'caption' => $data['caption'],
  'url' => $data['url'],
  'image' => $imagePath,
]);

您刚刚更新了经过身份验证的用户的所有相关帖子。对于特定帖子,您只需更新该特定实例。 如果您当前的代码运行良好,那么我相信在进行微小更改之后也应该可以运行。

【讨论】:

以上是关于为啥当我编辑并保存对一个帖子的更改时,所有其他帖子都会进行相同的更改?我该如何解决?我正在使用 laravel 8的主要内容,如果未能解决你的问题,请参考以下文章

Summernote 在 Angular 8 中无法正确更改内容

tinyMCE.editors[] wordpress 4.8 未定义

为啥当我使用 React 钩子和 React 上下文添加新帖子时,我的状态会替换我的帖子

Wordpress“编辑帖子”分页符

编写帖子并将其与1000个站点自动同步的最快方式

为啥我总是在这个 ajax 帖子到 php 时得到未定义的响应?