为什么当我编辑并保存对一个帖子的更改时,所有其他帖子都进行相同的更改?我该如何解决?我正在使用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

查看:40
本文介绍了为什么当我编辑并保存对一个帖子的更改时,所有其他帖子都进行相同的更改?我该如何解决?我正在使用laravel 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Whenever I try to save changes to only one post, the outcome is that all other posts I have take on those same changes.

我在web.php中的路线是:

My routes in web.php are:

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中的编辑功能是:

My edit function in Posts Controller is:

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

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

My update function in Posts Controller is:

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文件中,我的表单是:

My form in edit-post.blade.php file is:

<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文件,我想在其中将帖子的数据输出到:

And finally, here is the profile.blade.php file, where I would like to output the posts' data to:

@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

推荐答案

在您的控制器中,将其替换为:

In your controller replace this:

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

与此:

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

您刚刚更新了经过身份验证的用户的所有相关帖子.对于特定帖子,您只需要更新该特定实例.如果您当前的代码运行良好,那么在进行了很小的更改后,我也认为该代码也将正常工作.

There you've just updated all the related posts of authenticated user. For the specific post you need to update that specific instance only. If your current code is working well, then I believe that should be work after that small change as well.

这篇关于为什么当我编辑并保存对一个帖子的更改时,所有其他帖子都进行相同的更改?我该如何解决?我正在使用laravel 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆