Php laravel中的多对多第三列种子

Php laravel中的多对多第三列种子,php,laravel,Php,Laravel,我有两个表,分别是用户和组。它们之间有多对多的关系。实际上,我想存储组中的哪个用户信息和组中的用户角色。所以我创建了一个迁移表,组用户表 public function up() { Schema::create('group_user', function (Blueprint $table) { $table->integer('user_id')->unsigned(); $table->integer('group_id')-&g

我有两个表,分别是用户和组。它们之间有多对多的关系。实际上,我想存储组中的哪个用户信息和组中的用户角色。所以我创建了一个迁移表,组用户表

public function up() {
    Schema::create('group_user', function (Blueprint $table) {
        $table->integer('user_id')->unsigned();
        $table->integer('group_id')->unsigned();

        $table->foreign('user_id')->references('id')->on('users');
        $table->foreign('group_id')->references('id')->on('groups');
    });
}
我如何播种:

$groupQuantity = 26;
$userQuantity = 20;

factory(User::class, $userQuantity)->create();

factory(Group::class, $groupQuantity)->create()->each(function ($foo) {
        $foo->users()->sync(
            User::all()->random(rand(1,20))
        );
});
它工作得非常完美,但我想在pivot_表中添加用户角色,如:

$table->integer('type')->unsigned()->comment('0- Owner, 1- Admin, 2- User');
我可以添加迁移,但我不知道如何播种?(当我尝试设定种子时,它会给出一个错误,类型没有默认值。)

或者我应该为用户角色创建第二个透视表


Laravel 5.4…

您可以通过添加DB函数来修改group_用户的播种器

for($i=0;$i<rand(0,10),$i++) {
    $user = factory(User::class, $userQuantity)->create();
    factory(Group::class, $groupQuantity)->create()->each(function ($foo) {
        DB::table('group_user')->insert([
            [
                'user_id' => $user->id,
                'group_id' => $foo->id,
                'type'    => rand(0,2)
            ],
        ]);        
    });         
}
for($i=0;$icreate();
工厂(组::类,$groupQuantity)->create()->each(函数($foo){
DB::table('group_user')->insert([
[
'user_id'=>$user->id,
'group_id'=>$foo->id,
'type'=>rand(0,2)
],
]);        
});         
}
尝试
$table->integer('type')->unsigned()->comment('0-Owner,1-Admin,2-User')->默认值(0);