我有表'角色'與字段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'
}