$responsible_users = collect($request->responsible_users)->map(fn ($user_id) => new OrganizationElementUser(['user_id' => $user_id, 'organization_element_id' => $organization_element_id, 'type' => DeviceAuthAndHierarchyElementRole::RESPONSIBLE_ROLE]));
$subordinate_users = collect($request->subordinate_users)->map(fn ($user_id) => new OrganizationElementUser(['user_id' => $user_id, 'organization_element_id' => $organization_element_id, 'type' => DeviceAuthAndHierarchyElementRole::DIRECT_SUBORDINATE_ROLE]));
$internal_users = $responsible_users->merge($subordinate_users)->toArray();
OrganizationElementUser::upsert($internal_users, ['user_id', 'organization_element_id', 'type'], ['user_id', 'organization_element_id', 'type']);
为什么我的upsert创建重复记录?
我的user_id
、organization_element_id
、type
字段可以单独重复,但所有3个字段组合在一起会创建唯一记录
例如,我想要的是:
user_id == 1 && organization_element_id == 2 && type == 'test'
//ignore if true otherwise insert
1条答案
按热度按时间vxf3dgd41#
您可以使用
UpdateOrCreate
方法。文档在这里。