Laravel updateOrCreate with auto-incremental database [英] Laravel updateOrCreate with auto-incremental database
问题描述
如果该值存在,我的目的是更新,否则在提交表单后,在数据库表中插入一个新行。
My purpose is to update if the value exists, else inserts a new row in the database table after submitting the form.
问题是,这里的功能在db表中添加新列,而不是更新它们。
The problem is, the function here adds new columns in db table instead of updating them.
这是我的功能:
MyModel::updateOrCreate(array(
'myField' => 'myValue',
))
->where('myAutoIncrementalField', '=', '5')
->where('myPrimaryKey', '=', '8');
我的数据库表是这样的:
1. myPrimaryKey(not auto
2. myAutoIncrementalField(自动增量,不能在模型上填充。)
My database table is like that :
1. myPrimaryKey (not auto incremental and is fillable on model.)
2. myAutoIncrementalField (auto incremental and cannot be fillable on model.)
谢谢你提前。
推荐答案
这是你如何使用这种方法:
This is how you use this method:
Model::updateOrCreate(
['primary_key' => 8],
['field' => 'value', 'another_field' => 'another value']
);
作为第一个参数传递一个唯一的字段数组,或者在您的情况下,主键。非唯一的领域在这里显然没有意义,就像传递任何东西与PK一样。
As 1st param pass an array of fields that are unique, or in your case, the primary key. Non-unique fields don't make sense here obviously just like passing anything along with the PK.
第二个参数是一组应该更新/创建的值,但在唯一/ pk搜索中被忽略。
2nd param is an array of values that should be updated/created too, but being ignored in the unique/pk search.
这篇关于Laravel updateOrCreate with auto-incremental database的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!