Laravel:如何更新“hasMany”相关模型?

时间:2014-02-24 16:12:09

标签: php web laravel

这是我的代码:

<?php namespace Models;

class CardBizSetting extends BaseModel {

  protected $table = 'mp_member_biz_setting';
  public $timestamps = false;
  public static $unguarded = true;


  public function wechatUsers() {
    return $this->hasMany('Models\WcMemberUser', 'bizid');
  }

  public function exchangesHistories() {
    return $this->hasMany('Models\WcMemberCashHistory', 'exchange_bizid');
    // ->where('exchange_wechat_id',$this->wc_openid);
  }

  public function chargeHistories() {
    return $this->hasMany('Models\WcMemberCharge', 'charge_bizid');
    // ->where('charge_wechatid',$this->wc_openid);
  }

  // And this function doesn't work--->
  public static function updateWechatUserStatus($bizid, $wechatopenid, $status) {
    $user = self::find($bizid)->wechatUsers()->where('wc_openid', $wechatopenid)->first();
    if ($user) {
      $user->update(array('wc_ustatus' => $status));
    }
  }
}

并且此功能不起作用

Models\CardBizSetting::updateWechatUserStatus(1,'www',1);

那么,我的问题是如何更新hasMany相关的子模型? 非常感谢你!

1 个答案:

答案 0 :(得分:1)

我在方法updateWechatUserStatus中没有看到任何错误。您是否检查过范围wechatUsers是否返回了您的期望?尝试调试代码,您将看到问题的来源。