選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DatabaseSeeder.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Database\Seeders;
  3. // use Illuminate\Database\Console\Seeds\WithoutModelEvents;
  4. use App\Models\Setting\Currency;
  5. use App\Models\User;
  6. use Illuminate\Database\Seeder;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Schema;
  9. class DatabaseSeeder extends Seeder
  10. {
  11. /**
  12. * Seed the application's database.
  13. */
  14. public function run(): void
  15. {
  16. $startDate = today()->startOfYear();
  17. $endDate = today();
  18. // Change Company Name to ERPSAAS after Login
  19. $firstCompanyOwner = User::factory()
  20. ->withPersonalCompany()
  21. ->create([
  22. 'name' => 'Admin',
  23. 'email' => 'admin@gmail.com',
  24. 'password' => bcrypt('password'),
  25. 'current_company_id' => 1,
  26. 'created_at' => $startDate->copy(),
  27. ]);
  28. $firstCompanyOwner->ownedCompanies->first()->update(['created_at' => $startDate->copy()]);
  29. // Function to create employees for a company (also creates companies for the employees)
  30. $createUsers = static function ($company_id, $userCount, $minPercentage, $maxPercentage) use ($endDate, $startDate) {
  31. $users = User::factory($userCount)
  32. ->withPersonalCompany()
  33. ->create([
  34. 'password' => bcrypt('password'),
  35. 'current_company_id' => $company_id,
  36. ]);
  37. $dateRange = $endDate->diffInMinutes($startDate);
  38. $minOffset = (int) ($dateRange * $minPercentage);
  39. $maxOffset = (int) ($dateRange * $maxPercentage);
  40. for ($i = 0; $i < $userCount; $i++) {
  41. $increment = (int) ($minOffset + ($i * ($maxOffset - $minOffset) / $userCount));
  42. $userCreatedAt = $startDate->copy()->addMinutes($increment);
  43. $user = $users[$i];
  44. // Randomly assign a role to the user
  45. $roles = ['editor', 'admin'];
  46. $role = $roles[array_rand($roles)];
  47. $user->companies()->attach($company_id, compact('role'));
  48. $user->update(['created_at' => $userCreatedAt]);
  49. $user->ownedCompanies->first()?->update(['created_at' => $userCreatedAt]);
  50. // Generate random created_at date for the company_user pivot table (for employees)
  51. $user->companies->first()?->users()->updateExistingPivot($user->id, ['created_at' => $userCreatedAt]);
  52. }
  53. };
  54. // Users for the first company (excluding the first company owner)
  55. $createUsers(1, 5, 0, 0.1);
  56. // Second company owner
  57. $secondCompanyOwner = User::factory()
  58. ->withPersonalCompany()
  59. ->create([
  60. 'password' => bcrypt('admin2'),
  61. 'current_company_id' => 2,
  62. 'created_at' => $startDate->addMinutes($endDate->diffInMinutes($startDate) * 0.1),
  63. ]);
  64. $secondCompanyOwner->ownedCompanies->first()->update(['created_at' => $startDate->addMinutes($endDate->diffInMinutes($startDate) * 0.1)]);
  65. // Users for the second company (excluding the second company owner)
  66. $createUsers(2, 5, 0.1, 0.2);
  67. // Third company owner
  68. $thirdCompanyOwner = User::factory()
  69. ->withPersonalCompany()
  70. ->create([
  71. 'password' => bcrypt('admin3'),
  72. 'current_company_id' => 3,
  73. 'created_at' => $startDate->addMinutes($endDate->diffInMinutes($startDate) * 0.2),
  74. ]);
  75. $thirdCompanyOwner->ownedCompanies->first()->update(['created_at' => $startDate->addMinutes($endDate->diffInMinutes($startDate) * 0.2)]);
  76. // Users for the third company (excluding the third company owner)
  77. $createUsers(3, 5, 0.2, 0.3);
  78. // Create employees for each company (each employee has a company)
  79. $createUsers(4, 5, 0.3, 0.4);
  80. $createUsers(5, 5, 0.4, 0.5);
  81. $createUsers(6, 5, 0.5, 0.6);
  82. $createUsers(7, 5, 0.6, 0.7);
  83. $createUsers(8, 5, 0.7, 0.8);
  84. $createUsers(9, 5, 0.8, 0.9);
  85. $createUsers(10, 5, 0.9, 1);
  86. }
  87. }