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

Randy_Johnson's avatar

Database Diagram

Hi, I am creating a website but I am not quite sure if I have the database design correctly.

https://ibb.co/44n9tCv

class Attendance extends Model
{
    use HasFactory;

    public function classroom()
    {
        return $this->hasOne(Classroom::class, 'classroom_id', 'id');
    }
}
class Classroom extends Model
{
    use HasFactory;

    public function student()
    {
        return $this->hasMany(Student::class, 'student_id', 'id');
    }

    public function teacher()
    {
        return $this->hasMany(Teacher::class, 'teacher_id', 'id');
    }
}
class Student extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'surname',
    ];

    public function classroom()
    {
        return $this->hasOne(Classroom::class, 'student_id');
    }
}
class Teacher extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'surname',
    ];

    public function classroom()
    {
        return $this->hasOne(Classroom::class, 'teacher_id');
    }
}

I am also trying this:

        $this->students = DB::table('students')
            ->join('classrooms', function ($join) {
                $join->on('classrooms.student_id', '=', 'students.id')
                ->where('classroom.teacher_id', '=', $this->teacherId);
            })->get();

        dd($this->students);

But I am getting the error of:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'classroom.teacher_id' in 'on clause' (SQL: select * from `students` inner join `classrooms` on `classrooms`.`student_id` = `students`.`id` and `classroom`.`teacher_id` = 10) 

I must add its important that I just use query builder, Eloquent is too much magic for me.

0 likes
1 reply
MichalOravec's avatar

classrooms.teacher_id and not classroom.teacher_id you miss s in the word classroom

1 like

Please or to participate in this conversation.