Laravel 5.7
你好,我在 Stack Overflow 上查找了许多可能的答案,但没有得出结论。我正在通过简单的检查更新一个 patron 表中的简单整数值并且它没有更新数据库。我已经尝试在模型上使用 save() 和 update() 方法。
没有任何错误或异常显示,并且 save() 和 update() 方法返回 true。
代码:
控制器类使用模型并更新数据:
$patron_coupon = PatronCoupons::where('owner',$patron_id)->where('coupon_id',$active_coupon['coupon_id'])->first();
if($patron_coupon->current_uses > $active_coupon['max_uses'])
$patron_coupon->current_uses = $active_coupon['max_uses'];
// if i echo $patron_coupon->current_uses here, it will show that this value has changed ( good )
$patron_coupon->current_uses = $active_coupon['max_uses'] - $patron_coupon->times_used;
if($patron_coupon->current_uses < 0)
$patron_coupon->current_uses = 0;
// this doesnt work
$patron_coupon->save();
// this doesnt work either
$patron_coupon->update($patron_coupon->toArray());
// if i echo current_uses here, the value goes back to what it was before being set.
模型类:
class PatronCoupons extends Model
{
protected $table = 'patron_coupons';
protected $primaryKey = 'owner';
public $timestamps = false;
protected $fillable = ['times_used','current_uses','settings_version'];
}
迁移文件:
Schema::create('patron_coupons', function (Blueprint $table) {
$table->string('owner'); // patron id that 'owns' this coupon
$table->unsignedInteger('coupon_id'); // id of this coupon
$table->unsignedInteger('current_uses')->default(0); // the amount of uses this patron has for this coupon
$table->unsignedInteger('settings_version')->nullable();// last settings version fetch
$table->unsignedInteger('times_used')->default(0); // amount of times this coupon has been used
});
请帮帮我,我已经在桌子上磕了几个小时了!!!!
update([ 'current_uses' => 12 ])
? - Jonathan Ksave()
呢? - Zacvendor
和composer.lock
文件,然后再次运行composer install
。也许会起作用。 - Jonathan Kcomposer install
,但仍然不能与我的原始代码一起正常工作。我将坚持使用静态函数,这是我真正需要的。谢谢大家。 - Zac