Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tedo12334@gmail.com's avatar

Set course_id to the relevant id of a course

Hello i encountered this problem: While trying to seed specific grades to specific courses i am not able to connect the different grades to a specific course:

This is the GradeSeeder

$courseId = DB::table('courses')->pluck('id');

        $grades = [
            [
                'course_id' => $courseId->get('id'),
                'exam' => 'Report',
                'lowest_passing_grade' => 5.5,
                'best_grade' => 10,
            ],
            [
                'course_id' => $courseId->get('id'),
                'exam' => 'Assessment',
                'lowest_passing_grade' => 5.5,
                'best_grade' => 10,
            ]
 		];

        foreach ($grades as $grade) {
            Grade::create($grade);
        }

And the Course model:

public function grades()
    {
        return $this->hasMany(Grade::class);
    }

the grade model

public function course()
    {
        return $this->belongsTo(Course::class);
    }

this is the grades migration

Schema::create('grades', function (Blueprint $table) {
            $table->id();
            $table->foreignId('course_id')->references('id')->on('courses');
            $table->string('exam');
            $table->decimal('lowest_passing_grade')->default(5.5);
            $table->decimal('best_grade')->nullable();
            $table->timestamps();
        });

and this is my controller method

public function index()
    {
        $grades = DB::table('grades')
            ->join('courses', 'courses.id', '=', 'grades.course_id')
            ->select('courses.name', 'grades.*')
            ->get();

        return view('grades.index', compact('grades'));
    }
0 likes
8 replies
Sinnbeck's avatar

This will get multiple course ids

$courseId = DB::table('courses')->pluck('id');

instead you should just get one (maybe a random one?)

$courseId = DB::table('courses')->inRandomOrder()->value('id');
tedo12334@gmail.com's avatar

@Sinnbeck my goal was to refer to a specific id, not a random one, for example: course_id = course.id = 1 and the next one to be with id 2 and so on

Sinnbeck's avatar

@tedo12334@gmail.com So just set one?

$grades = [
            [
                'course_id' => 1,
                'exam' => 'Report',
                'lowest_passing_grade' => 5.5,
                'best_grade' => 10,
            ],
            [
                'course_id' => 2,
                'exam' => 'Assessment',
                'lowest_passing_grade' => 5.5,
                'best_grade' => 10,
            ]
 		];
tykus's avatar
tykus
Best Answer
Level 104

@tedo12334@gmail.com in that case; use the shift Collection method:

$courseId = DB::table('courses')->pluck('id');

$grades = [
    [
        'course_id' => $courseId->shift(),
            'exam' => 'Report',
            'lowest_passing_grade' => 5.5,
            'best_grade' => 10,
        ],
1 like
tykus's avatar

What are you expecting this to do?

$courseId->get('id'),
Nakov's avatar

How is this specific:

$courseId = DB::table('courses')->pluck('id');

? What you are doing here is getting all the IDs for all the courses you have in your database.

And you can do this:

public function index()
{
    $grades = Grade::with('course')->get();

    return view('grades.index', compact('grades'));
}

Please or to participate in this conversation.