2016-12-02 123 views
0

我試圖將更改保存到一個數據庫表中的記錄,該數據庫表具有2個元素的組合鍵。 當我嘗試使用Model :: updateOrCreate或Model :: firstOrCreate方法時,我從數據庫中得到一個錯誤,它告訴我該ID不存在。 看起來像laravel,創建查詢時,忽略了我提到的主鍵。Laravel雄辯updateOrCreate組合鍵

我的模型:

class CatalogueCatLanguage extends Model 
{ 
    public $incrementing = false; 
    protected $table = "CatalogueCatLanguages"; 
    protected $fillable = ["cataloguecategory_id","language_id","name","description"]; 
    protected $primaryKey = ["cataloguecategory_id","language_id"]; 
} 

我的控制器方法中的代碼:

CatalogoCatLingua::updateOrCreate(
[ 
    "cataloguecategory_id"=>$trad["category"], 
    "language_id"=>$trad["language"] 
],[ 
    "name"=>$trad["langName"], 
    "description"=>$trad["langDescription"] 
] 
); 

錯誤我是說我有 「的PrimaryKey」 屬性:

非法偏移類型在isset or empty

呃ROR我是說我有 「的PrimaryKey」 屬性評論:

SQLSTATE [42S22]:列未找到:在 'where子句'

我已經嘗試過多種組合1054未知列 'ID'和這樣做的方式,但我沒有得到什麼導致這個錯誤。

謝謝,提前。

回答

0

Laravel不支持複合主鍵(您可以嘗試this解決方案)。您只能在此處使用字符串:

protected $primaryKey = "cataloguecategory_id";