如何写“重复密钥更新"?对于Laravel 5.2雄辩的模型? [英] How to write "on duplicate key update" for Laravel 5.2 eloquent model?
问题描述
我有一张桌子,它的唯一键是app_id + channel_id
,但没有主键.
I have a table, it's unique key is app_id + channel_id
but it doesn't have primary key.
我想使用on duplicated key update
语句在Laravel
雄辩的模型中插入/更新last_update_time
字段,我找到了updateOrCreate
方法,但是在我的项目中无法识别,我该如何编写模型?>
I want to use on duplicated key update
statement to insert/update a last_update_time
field in Laravel
eloquent model, I found updateOrCreate
method but it's not recognized in my project, how should I write my model?
推荐答案
DB::statement("INSERT INTO `table_name`(`col_name_1`, `col_name_2`) VALUES (?, ?) ON DUPLICATE KEY UPDATE col_name_1= VALUES (col_name_1)", [val_1, val_2]);
Laravel允许使用DB::statment($query, $bindings = [])
的原始数据库语句.
Laravel allows for raw database statements with DB::statment($query, $bindings = [])
.
要确保的一点是,第二个参数必须是索引数组,而不是关联数组.如果是关联的,将抛出一般的SQL异常.
One thing to make sure of, the second argument must be an indexed array and not an associative. If it is associative it will throw a general SQL exception.
这篇关于如何写“重复密钥更新"?对于Laravel 5.2雄辩的模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!