【问题标题】:How to only select one column and update it?如何只选择一列并更新它?
【发布时间】:2018-12-28 18:50:22
【问题描述】:

我只想从数据库中选择一列,然后只更新那一列

  DB::table('available_rooms')
  ->where('roomTypeID', '=', $request->session()->get('room_type'))->first()
  ->update(['isAvailable'=> 0]);

这样我得到一个错误

调用未定义的方法 stdClass::update()

【问题讨论】:

  • 我收到此错误 Call to undefined method stdClass::update()
  • 你是这个意思吗? DB::table('available_rooms') ->where('roomTypeID', '=', $request->session()->get('room_type')) ->update(['isAvailable'=> 0]) ->第一();我收到此错误,而不是在整数上调用成员函数 first()

标签: sql laravel eloquent


【解决方案1】:

雄辩的风格

如果您没有创建模型,请创建:

AvailableRoom::where('roomTypeID', '=',  $request->session()->get('room_type'))
              ->update(['isAvailable'=> 0])

【讨论】:

    【解决方案2】:

    使用limit() 方法代替first()

    DB::table('available_rooms')
      ->where('roomTypeID', '=', $request->session()->get('room_type'))->limit(1)
      ->update(['isAvailable'=> 0]);
    

    【讨论】:

      【解决方案3】:

      你可以试试这个:

      Post::where('id', $id)->update(array('title' => 'asdasd'));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-29
        • 1970-01-01
        • 2022-01-21
        • 1970-01-01
        • 2015-02-21
        • 1970-01-01
        • 2016-09-05
        相关资源
        最近更新 更多