Laravel 无法更新除已创建的第一行以外的其他表行
我在Laravel中创建应用程序时遇到了这个问题。用户需要能够编辑文章,但当点击编辑按钮时,它只会更新创建的第一篇文章。无法访问其他帖子。我怎样才能使它更新我按下按钮的那篇文章呢。这里有什么问题 edit.blade.phpLaravel 无法更新除已创建的第一行以外的其他表行,laravel,Laravel,我在Laravel中创建应用程序时遇到了这个问题。用户需要能够编辑文章,但当点击编辑按钮时,它只会更新创建的第一篇文章。无法访问其他帖子。我怎样才能使它更新我按下按钮的那篇文章呢。这里有什么问题 edit.blade.php <form action="/profile/{{ $post->id }}" enctype="multipart/form-data" method="post"> @csrf @method('PATC
<form action="/profile/{{ $post->id }}" enctype="multipart/form-data" method="post">
@csrf
@method('PATCH')
...edit form here...
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);
}
}
我想你得先找到它 像这样
$post = Post::find($post->id);
$post->update($data);
在控制器中,您只是验证请求,而不是更新帖子。只是忘记添加行。但是它是在那里的。路由模型绑定是否如预期的那样工作?我需要传递正确的post id才能正常工作。我该如何做才能少做些改动呢?这没用。我也在帖子中插入了我当前的模型
$post = Post::find($post->id);
$post->update($data);