42 lines
1.1 KiB
PHP
42 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\AcademicYear;
|
|
use App\Models\ClassRoom;
|
|
use App\Models\ClassSubject;
|
|
use App\Models\Subject;
|
|
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
|
use Illuminate\Database\Seeder;
|
|
|
|
class ClassSubjectSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*/
|
|
public function run(): void
|
|
{
|
|
$classRooms = ClassRoom::all();
|
|
$subjects = Subject::all();
|
|
$academicYear = AcademicYear::where('is_active', true)->first();
|
|
|
|
if (!$academicYear) {
|
|
$this->command->error('Tahun ajaran aktif tidak ditemukan.');
|
|
return;
|
|
}
|
|
|
|
foreach ($classRooms as $classRoom) {
|
|
// Contoh: ambil 3 sampai 5 mapel random untuk tiap kelas
|
|
$assignedSubjects = $subjects->random(rand(3, 5));
|
|
|
|
foreach ($assignedSubjects as $subject) {
|
|
ClassSubject::firstOrCreate([
|
|
'class_room_id' => $classRoom->id,
|
|
'subject_id' => $subject->id,
|
|
'academic_year_id' => $academicYear->id,
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|