【问题标题】:Unable to update other table rows other than 1st created无法更新第一次创建以外的其他表行
【发布时间】:2019-06-24 16:54:48
【问题描述】:

我在 Laravel 中创建应用程序时遇到了这个问题。用户需要能够编辑帖子,但是当点击编辑按钮时,它只会更新创建的第一个帖子。无法访问其他帖子。我怎样才能让它更新我按下按钮的确切帖子。这可能是什么问题?

edit.blade.php

<form action="/profile/{{ $post->id }}" enctype="multipart/form-data" method="post">
            @csrf
            @method('PATCH')
...edit form here...

web.php

Route::patch('/profile/{post}', 'PostsController@update')->name('profile.update');

PostsController.php

public function update(Post $post)
    {
        $data = request()->validate([
            'category' => 'required',
            'description' => 'required',
            'price' => 'required',
            'image' => 'image',
        ]);
        $post->update($data);
        return redirect("/profile/".auth()->user()->id);
    }

后模型 命名空间应用程序;

use Illuminate\Database\Eloquent\Model;


class Post extends Model
{
    protected $guarded = [];
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

【问题讨论】:

  • 在您的控制器中,您只是验证请求而不是更新帖子。
  • 刚刚忘记添加该行。但它以前就在那里
  • 路由模型绑定是否按预期工作?
  • 我需要传入正确的帖子 ID 才能正常工作。我该如何做才能做的更改可能更少?

标签: laravel


【解决方案1】:

我想你必须先找到它 像这样

 $post = Post::find($post->id);
 $post->update($data);

【讨论】:

  • 它没有帮助。我也在帖子中插入了我当前的模型
猜你喜欢
  • 2019-09-23
  • 2022-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多