Laravel 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.
模型类:
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
});
请帮助,我一直在小时上敲我的头!
答案 0 :(得分:4)
您可以尝试以下任何一种方法:
尝试改用静态update
函数
PatronCoupons::update([
// updates here
]);
删除您的vendor
文件夹和composer.lock
文件。然后运行composer install
刷新您的供应商文件
答案 1 :(得分: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);