43 lines
1.1 KiB
PHP
43 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\Feed;
|
|
use App\Models\FeedSchedule;
|
|
use Illuminate\Database\Seeder;
|
|
|
|
class FeedScheduleSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*/
|
|
public function run(): void
|
|
{
|
|
$times = ['07:00', '12:00', '16:00'];
|
|
$feeds = Feed::all();
|
|
|
|
if ($feeds->isEmpty()) {
|
|
$this->call(FeedSeeder::class);
|
|
$feeds = Feed::all();
|
|
}
|
|
|
|
foreach ($times as $time) {
|
|
$schedule = FeedSchedule::firstOrCreate([
|
|
'scheduled_at' => $time,
|
|
]);
|
|
|
|
// Attach 1-2 random feeds to each schedule
|
|
$selectedFeeds = $feeds->random(rand(1, 2));
|
|
|
|
foreach ($selectedFeeds as $feed) {
|
|
// Check if already attached to avoid duplicates if re-run
|
|
if (! $schedule->feeds()->where('feed_id', $feed->id)->exists()) {
|
|
$schedule->feeds()->attach($feed->id, [
|
|
'quantity' => rand(10, 50),
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|