您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CategorySeeder.php 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Database\Seeders;
  3. use App\Models\Setting\Category;
  4. use Illuminate\Database\Console\Seeds\WithoutModelEvents;
  5. use Illuminate\Database\Seeder;
  6. use Illuminate\Support\Facades\DB;
  7. class CategorySeeder extends Seeder
  8. {
  9. /**
  10. * Run the database seeds.
  11. */
  12. public function run(): void
  13. {
  14. $companyId = DB::table('companies')->first()->id;
  15. $userId = DB::table('users')->first()->id;
  16. $incomeCategories = [
  17. 'Salary',
  18. 'Bonus',
  19. 'Interest',
  20. 'Dividends',
  21. 'Rentals',
  22. ];
  23. $expenseCategories = [
  24. 'Rent',
  25. 'Utilities',
  26. 'Food',
  27. 'Transportation',
  28. 'Entertainment',
  29. ];
  30. // Merge and shuffle the sales and purchase taxes
  31. $shuffledCategories = [
  32. ...array_map(static fn($name) => ['name' => $name, 'type' => 'income'], $incomeCategories),
  33. ...array_map(static fn($name) => ['name' => $name, 'type' => 'expense'], $expenseCategories)
  34. ];
  35. shuffle($shuffledCategories);
  36. $allCategories = $shuffledCategories;
  37. // Create each category
  38. foreach ($allCategories as $category) {
  39. Category::factory()->create([
  40. 'company_id' => $companyId,
  41. 'name' => $category['name'],
  42. 'type' => $category['type'],
  43. 'enabled' => false,
  44. 'created_by' => $userId,
  45. 'updated_by' => $userId,
  46. ]);
  47. }
  48. // Set the first income category as enabled
  49. Category::where('type', 'income')
  50. ->where('company_id', $companyId)
  51. ->first()
  52. ->update(['enabled' => true]);
  53. // Set the first expense category as enabled
  54. Category::where('type', 'expense')
  55. ->where('company_id', $companyId)
  56. ->first()
  57. ->update(['enabled' => true]);
  58. }
  59. }