我正在创建一个编辑功能,可以编辑帖子的标题。实际上,它可以成功更新记录,但是当我只编辑其中一个时,它会更新所有帖子的标题。当我提交编辑请求时,它立即更改了所有帖子。
该项目位于Php Laravel之下,并使用sqlite进行数据库管理。
PostsController.php
public function update(Post $post){
auth();
$userid = auth()->user()->id;
$postuid = $post->user_id;
if ($postuid == $userid) {
$data = request()->validate([
'caption' => 'required',]);
auth()->user()->posts()->update($data);
return redirect("/p/{$post->id}");
}else return abort('403');
}
Create_posts_table.php
Schema::create('posts',function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->text('caption');
$table->string('image');
$table->timestamps();
$table->index('user_id');
});
create_user_table.php
Schema::create('users',function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->string('username')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
预期:如果我访问其记录页面,它将更新每条记录。 实际:访问其中一个编辑页面并立即更新所有记录。