调用函数后错误地更新了所有记录

我正在创建一个编辑功能,可以编辑帖子的标题。实际上,它可以成功更新记录,但是当我只编辑其中一个时,它会更新所有帖子的标题。当我提交编辑请求时,它立即更改了所有帖子。

该项目位于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();
});

预期:如果我访问其记录页面,它将更新每条记录。 实际:访问其中一个编辑页面并立即更新所有记录。

yuanpeichen 回答:调用函数后错误地更新了所有记录

auth()->user()->posts()->update($data);将更新与当前posts()关联的所有auth()->user()。如果您只想更新当前的Post,只需

$post->update($data);

通过模型中的Post $post进行路由模型绑定,将在URL中找到一个与ID相关的Post实例,因此您只需对其进行更新即可。

,

这样做:

public function update(Post $post){
    auth();
    $userid = auth()->user()->id;
    $postuid = $post->user_id;

    if ($postuid == $userid) {
        $data = request()->validate([
            'caption' => 'required',]);

        // update only the specific post,not all posts belonging to user
        $post->update($data);

        return redirect("/p/{$post->id}");
    }else return abort('403');
}
,

在没有一定安全性的情况下,请勿使用方法update

开发人员的第一条规则是永远不要信任客户的输入。例如,如果请求中包含输入user_id的值不是当前用户的ID,则您的代码将更改帖子的所有者。

在这种情况下,没有敏感字段,但是...

这是一个很好的示例(将在代码中进行解释):

public function update($postId){ //remove model injection and use the post ID.

    //validate data you can use FormRequest class extention in the method parameters instead
    $data = request()->validate([
        'caption' => 'required','not_required_field' => 'string',]);

    //get the post from the relation of the user,if the post doesnt exist or is not the user's post,it will return a 404 response.
    $user = auth()->user();
    $post = $user->posts()->findOrFail($postId);

    //get the data one variable at a time it's more safe. The input method second parameter is the default to set
    $caption = request()->input('caption');
    $notRequiredField = request()->input('not_required_field','');

    //now you can update
    $post->caption = $caption;
    $post->not_required_field = $notRequiredField;
    $post->save();

    //result of success
    return redirect("/p/{$post->id}");
}

不要忘记将路由中的{$post}更改为{$postId}

本文链接:https://www.f2er.com/3158040.html

大家都在问