admin表,role表, 如何在admin_role表中插入关联id?
1、建模
1 // cmd 2 php artisan make:model Admin // Admin model 3 4 php artisan make:model Role // Role moel
2、Admin model
1 class Admin extends Role 2 { 3 public function role() 4 { 5 return $this->belongsToMany('AppRole', 'admin_role', 'admin_id', 'role_id'); 6 } 7 }
$this->belongsToMany('关联模型',‘中间表’,‘外键’, ‘关系外键’);
3、Role model
4、插入数据
1 use AppAdmin; 2 $id = DB::table('admin')->insertGetId($arr); // 返回插入的id 3 $admin=Admin::find($id); 4 $admin->role()->attach($adminRole);