3

拉拉维尔 5.7

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

我正在通过简单的检查更新赞助人表中的简单整数值,它没有更新数据库。我已经尝试在模型上使用save()update()方法。

没有出现错误或异常,save()andupdate()方法返回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
});

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

4

2 回答 2

3

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

  1. 尝试改用静态update函数

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

于 2019-05-16T18:09:55.677 回答
1

获取记录

$patronCoupons = PatronCoupons::find($id);

$patronCoupons->update([
    'times_used' => 'value',
    'current_uses' => 'value',
    'settings_version' => 'value'
]);

如果您的帖子数据具有相同的输入名称,则直接使用

$post = $request->all();

$patronCoupons = PatronCoupons::find($id);

$patronCoupons->update($post);

于 2019-05-17T07:27:54.500 回答