简体   繁体   English

laravel 8 使用 updateOrCreate 的复合主键

[英]laravel 8 Composite primary key with updateOrCreate

I just want to ask why upset method in eloquant ORM we can update records with "Composite" primary keys, and we can not do that with updateOrCreate?我只是想问一下为什么在 eloquant ORM 中的打乱方法我们可以用“复合”主键更新记录,而我们不能用 updateOrCreate 做到这一点? !!! !!! I have a table that users insert and update in most of times and it's not a good idea to have one Primary Key "id" !!我有一张用户在大多数情况下插入和更新的表,拥有一个主键“id”不是一个好主意!

if you are not using the model events you can Mute events for your action to avoid the error.如果您不使用 model 事件,您可以为您的操作静音事件以避免错误。 https://laravel.com/docs/8.x/eloquent#muting-events https://laravel.com/docs/8.x/eloquent#muting-events

ModelName::withoutEvents(function (){
    ModelName::updateOrCreate(
        [
            'id' => 'test',
            'second_id' => 0
        ],
        [
            'data' => 'some data'
        ]
    );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM