Laravel 模型 save() 和 update() 不保存

Zac*_*Zac 5 php laravel eloquent

拉拉维尔 5.7

您好,我一直在查看堆栈溢出并尝试了许多可能的答案,但没有得出任何结论。

我正在通过简单的检查更新顾客表中的一个简单整数值,但它不会更新数据库。我已经尝试过在模型上使用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.
Run Code Online (Sandbox Code Playgroud)

型号类别:

class PatronCoupons extends Model
{
    protected $table = 'patron_coupons';
    protected $primaryKey = 'owner';
    public $timestamps = false;
    protected $fillable = ['times_used','current_uses','settings_version'];
}
Run Code Online (Sandbox Code Playgroud)

迁移文件:

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
});
Run Code Online (Sandbox Code Playgroud)

请帮忙,我已经把头撞在桌子上好几个小时了!!!!

小智 3

您可以尝试以下任一方法:

  1. 尝试使用静态update函数代替

    PatronCoupons::update([
        // updates here
    ]);
    
    Run Code Online (Sandbox Code Playgroud)
  2. 删除您的vendor文件夹和composer.lock文件。然后运行composer install刷新您的供应商文件