Laravel 块结果具有多对多关系

时间:2021-03-16 19:52:45

标签: laravel-8

我想使用具有多对多关系的块插入 users 数据


for ($i = 0; $i < 1000; $i++) {
      $data[] = [
        'name'              => $faker->name,
        'email'             => $faker->unique()->safeEmail,
        'email_verified_at' => now(),
        'password'          => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi',
        'remember_token'    => Str::random(10),
      ];
    }



$chunks = array_chunk($data, 20);

foreach ($chunks as $chunk) {
      User::insert($chunk, function ($users) {
        foreach ($users as $user) {
          $user->attachRole('user');
        }
      });
    }

块正在工作,但它没有像我预期的那样将角色 user 附加到每条记录

0 个答案:

没有答案