Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CategorySeeder.php 1.9KB

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