dimanche 24 septembre 2017

check the database and insert if data not exist

I have the table 'role' with fields user_id and role_id.I have get the new value from the another query 154,516 from the for loop.I want to check 154 is in the role table user_id if not exit insert the new field or if exist update the field.In my case update the role_id for the user_id '154' and insert the new row for '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'
        }



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2yzg1mT
via IFTTT

Aucun commentaire:

Enregistrer un commentaire