【问题标题】:update column value in laravel 5.4在 laravel 5.4 中更新列值
【发布时间】:2019-03-16 11:18:14
【问题描述】:

我想更新帖子的隐私

我的控制器

   public function changePostsPrivacy(Request $request){
            $userId = $request->user()->id;
            $postid = $request->get('id');
            //dd($postid);
            $privacy = $request->get('privacy');//dd($privacy);
            $user = User::where(['id' => $userId, 'hide' => 0])->first();
            if($user && $postid && in_array($privacy, [1,0])){
                DB::table('posts')->update(['creator_id' => $userId, 'id' => $postid],[
                    'privacy' => $privacy,
                ]);
            }
        }

路线:

 Route::group(['middleware'=>['auth:api', \App\Http\Middleware\OnlyRegisteredUsers::class]], function(){
        /**
         * Group for registered users only APIs
         */
        Route::post('changePostsPrivacy','UserController@changePostsPrivacy');
    });

迁移

 public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->longText('content');
            $table->string('short_description');
            $table->unsignedInteger('media_id')->nullable();
            $table->foreign('media_id')->references('id')->on('medias');
            $table->unsignedInteger('creator_id');
            $table->foreign('creator_id')->references('id')->on('users');
            $table->boolean('hide')->default(0);
            $table->timestamps();
        });
    }

此迁移中添加了新列

 public function up()
        {
            Schema::table('posts', function (Blueprint $table) {
                $table->integer('privacy')->after('creator_id');
            });
        }

当我想为任何帖子添加隐私时,它会给我一个错误

“消息”:“SQLSTATE [23000]:完整性约束违规:1451 无法删除或更新父行:外键约束失败 (webdb.comments, 约束comments_post_id_foreign 外键 (post_id) 参考posts (id)) (SQL: 更新posts 设置 creator_id = 17, id = 48)",

【问题讨论】:

    标签: database laravel phpmyadmin postman database-migration


    【解决方案1】:

    你必须在更新隐私帖子中使用 where 条件

    DB::table('posts')->where(['creator_id' => $userId, 'id' => $postid])->update(['privacy' => $privacy])
    

    【讨论】:

      【解决方案2】:

      如果你有 Post 模型,你可以这样做:

      Post::where(['creator_id' => $userId, 'id' => $postid])->update(['privacy' => $privacy]);
      

      为防止任何外键错误,您应该使用validation request 来检查提供的user_idpost_id exists 是否存在。

      【讨论】:

      • 是的,我试过了,我从邮递员那里发送数据,当我发送 1、0 或空字段时它可以工作,但是当我传递任何其他数据时它会给我一个错误
      • 从 POSTMAN 发送数据通常会失败,因为您的路由必须使用 CSRF 令牌中间件进行保护。所以你需要一个有效的令牌来发布你的数据。
      【解决方案3】:
      public function changePostsPrivacy(Request $request){
                  $userId = $request->user()->id;
                  $postid = $request->get('id');
                  $privacy = $request->get('privacy');
                  $user = User::where(['id' => $userId, 'hide' => 0])->first();
                  if($user && $postid && in_array($privacy, [1,0])){
                      DB::table('posts')->update([
                          'privacy' => $privacy,
                      ]);
                  }
      

      创作者id和id不需要更新。

      【讨论】:

        【解决方案4】:

        你也可以试试这个。

        $post = Post::where(['creator_id' => $userId, 'id' => $postid])->first();
        $post->privacy = $privacy;
        $post->save();
        

        【讨论】:

          猜你喜欢
          • 2017-08-29
          • 1970-01-01
          • 2017-09-14
          • 1970-01-01
          • 1970-01-01
          • 2017-06-11
          • 2017-11-10
          • 1970-01-01
          • 2023-03-24
          相关资源
          最近更新 更多