在新 table 中获取并推送数据

fetch and push the data in new table

我有 table 'project'、'user' 和 'role_mapping'。我想从项目 table 中获取 project_owner_id并推送从用户 table 和 role_id=1 中获取的 user_id role_mapping table.one 项目所有者有多个 project.so 我想限制双重输入。我想在 laravel controller controller.

中完成
project table
project_id   project name project_owner_id
 1             DEC01
 2             DEC02
 3             DEC01

user table
 id user_string
 1     DEC03
 2     DEC01
 3     DEC02

role_mapping table
user_id  role_id
 2        1
 3        1     

$heads = DB::table('project')
      ->join('users', 'project.project_owner_id', '=', 'users.user_string')
      ->select('project.project_owner_id','users.id as user_id')->get();
      foreach($heads as $head){
          print_r($head->project_owner_id);  
          print_r($head->user_id);            
      }

以下查询获取了项目所有者 ID 和 user_id.How 以将值推送到 role_mapping table

尝试参考这个,使用 laravel 中的附加、分离:http://kaloraat.com/questions/laravel-attach-detach-and-sync-methods

希望这对您有所帮助:)

像这样

假设您的型号名称为 MappingTable

foreach($heads as $head){
   $mapTable = new MappingTable();
   $mapTable->user_id = $head->user_id;
   $mapTable->role_id = $head->project_owner_id;
   $mapTable->save();            
}

或者您可以像这样传递 $data 数组

$data = array(
    array('user_id'=>$head->user_id, 'role_id'=> ),
    array('user_id'=>$head->user_id, 'role_id'=> $head->project_owner_id),
    //...
);

MappingTable::insert($data); // Eloquent
DB::table('role_mapping table')->insert($data); // Query Builder