【问题标题】:How do I update data in laravel?如何更新 laravel 中的数据?
【发布时间】:2021-12-24 09:27:54
【问题描述】:

我正在尝试更新我已经存在的用户。但是我真的不知道该怎么做。

这是我用来更新用户的 API 路由

Route::put('/user/{user}', [UserController::class, 'updateUser']);

用户控制器中的函数是这样的:

public function updateUser(Request $request, User $user){
        $data = $request->validate([
            'username' => '',
            'firstname' => '',
            'lastname' => '',
            'email' => '',
            'password' => '',
            'phone_number' => '',
            'role' => '',
            'companyID' => '',
            'companyRole' => '',
        ]);

        $input = $request->all();
        $user->update($input);
        return $user;
    }

这是我在 vue 中用来将新用户数据发送到 laravel 的函数:

editUser(){
            console.log(this.user)
            this.$axios
                .put('api/user/' + this.$route.params.id + this.user)
                .then((response) => {
                    console.log(response.data)
                })
                .catch((err) => {
                    console.log(err)
                })
        }

这是“this.user”

{ "id": 1, "username": "user", "firstname": "user", "lastname": "user", "email": "user@test.com", "email_verified_at": null, "phone_number": 6282223, "role": "user", "companyID": 2, "companyRole": "employee", "logged_in": 0, "created_at": "2021-11-11T12:00:23.000000Z", "updated_at": "2021-11-11T12:00:23.000000Z" }

而“this.$route.params.id”只是用户 ID

我不知道我做错了什么。没有更新

【问题讨论】:

  • 您的意思是.put('api/user/' + this.$route.params.id, this.user)?您的原始代码似乎试图将this.user(一个对象)连接到 URL
  • @Phil 错了吗?
  • 您的原始代码将尝试向api/user/1[object%20Object] 发出 PUT 请求。可能不是你想要的
  • @Phil 好的,我该怎么做才能正常工作?
  • 我的第一条评论不清楚吗?如果之后仍然无法正常工作,请使用您可以使用的工具(尤其是浏览器开发工具 Network 面板)来调试问题

标签: laravel vue.js axios


【解决方案1】:

Laravel 参数绑定只需传递 id 即可为您提供用户。

这里的主要错误是您调用 API 的方式。

从此改变 axios:

this.$axios.put('api/user/' + this.$route.params.id + this.user)

到这里:

this.$axios.put(`api/user/${this.$route.params.id}`, this.user)

这样,laravel 将接收 ID 并将其转换为用户,同时接收 “this.user”中发送的有效负载

【讨论】:

    【解决方案2】:

    也许我会尝试另一种方法来更新具有特定 ID 的用户。

    在您定义路由的 web.php 文件中:

    Route::get('/update_user', '\App\Http\Controllers\PostController@update_user');
    

    在我的示例 PostController 中的控制器中:

    public function update_user()
    {
       //Find all users 
       $users = DB::select('select * from users');
    
       foreach($users as $user){
           //UPDATE
           DB::update('update users set email = "userTest@test.com" where id = ?',[4]);
       }
    
       //If you want echo an alert message for the result
       echo '<script>alert("The email of user with id = 4 has been updated.")</script>';
    
    }
    

    尝试根据您的需要来塑造代码,但基本思想是上面的。

    【讨论】:

      【解决方案3】:

      您不应该接受传入的用户,因为即使这样做,您(该人)也知道该用户的存在,而并非所有发送的用户都是如此。

      您应该只发送 ID,然后根据该 ID 在数据库中查找用户,然后只有当您确定用户存在时,它才应该更新。

      请求:

      editUser(){
                  console.log(this.user.id)
                  this.$axios
                      .put('/api/user/' + this.user.id)
                      .then((response) => {
                          console.log(response.data)
                      })
                      .catch((err) => {
                          console.log(err)
                      })
              }
      

      API 端点:

      Route::put('/user/{id}', [UserController::class, 'updateUser']);
      

      更新功能:

      public function updateUser(Request $request, $id){
          $data = $request->validate([
              'username' => '',
              'firstname' => '',
              'lastname' => '',
              'email' => '',
              'password' => '',
              'phone_number' => '',
              'role' => '',
              'companyID' => '',
              'companyRole' => '',
          ]);
      
          $input = $request->all();
          $user = User::findOrFail($id);
          $user->update($input);
          $user->save();
          return $user;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-02
        • 1970-01-01
        • 1970-01-01
        • 2019-10-15
        • 1970-01-01
        • 2018-06-11
        • 1970-01-01
        • 2017-06-08
        相关资源
        最近更新 更多