45 lines
1.1 KiB
PHP
45 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\Expense;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Seeder;
|
|
|
|
class ExpenseSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*/
|
|
public function run(): void
|
|
{
|
|
$users = User::all();
|
|
|
|
if ($users->isEmpty()) {
|
|
User::factory(5)->create();
|
|
$users = User::all();
|
|
}
|
|
|
|
// Create 30 random expenses over the last 6 months
|
|
Expense::factory(30)->create([
|
|
'created_at' => now()->subMonths(rand(0, 5))->subDays(rand(0, 28)),
|
|
]);
|
|
|
|
// Add some specific common expenses
|
|
$commonExpenses = ['Listrik Toko', 'Wifi Bulanan', 'Sampah Bulanan', 'Air'];
|
|
|
|
foreach (range(1, 6) as $month) {
|
|
$date = now()->subMonths($month)->startOfMonth();
|
|
|
|
foreach ($commonExpenses as $name) {
|
|
Expense::create([
|
|
'user_id' => $users->random()->id,
|
|
'name' => $name,
|
|
'amount' => rand(50000, 500000),
|
|
'created_at' => $date->copy()->addDays(rand(0, 28)),
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|