带有两个主键的 Laravel 模型更新
问题描述
我正在尝试更新具有两个主键的模型.
I'm trying to update Model which has two primary keys.
型号
迁移
这是应该更新库存模型的代码,但它没有.
This is code which should update Inventory model, but it doesn't.
我收到此错误:
我也尝试使用 updateOrCreate() 方法.它不起作用(我得到同样的错误).
I also tried to use updateOrCreate() method. It doesn't work (I get same error).
谁能告诉我们应该如何更新具有两个主键的模型?
Can anyone tell how Model with two primary key should be updated?
推荐答案
这个问题我遇到过好几次了.您需要覆盖一些属性:
I've run into this problem a couple of times. You need to override some properties:
和方法(信用):
记住这段代码需要引用 Eloquent Builder 类
Remember this code needs to reference Eloquent Builder class with
我建议将这些方法放在 HasCompositePrimaryKey
Trait 中,这样你就可以在任何需要它的模型中使用
它.
I suggest putting those methods in a HasCompositePrimaryKey
Trait so you can just use
it in any of your models that need it.
这篇关于带有两个主键的 Laravel 模型更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!