我有表'角色'与字段user_id和role_id.I已经从另一个查询154,516从for循环获取新值。我想检查154是在角色表user_id如果不退出插入新字段或存在更新字段。在我的情况下,更新user_id'154'的role_id并插入'516'的新行。检查数据库,并插入数据不存在
role //table
user_id role_id
210 1
125 2
310 1
154 1
foreach($heads as $head){
$first_count=$first_count+1;
$roleMappingObject = new role_mapping();
$roleMappingObject->user_id = $head->user_id;
$roleMappingObject->role_id = 1;
$roleMappingObject->save(); //this will insert the new row for both 154 and 516 but I want to update '154'
}