return $this->belongsTo(Student::class, 'student', 'student_number');
// and
return $this->hasMany(Assessment2::class, 'student', 'student_number');
Feb 18, 2020
6
Level 2
Eloquent Relationships
Hi all, I have a query regarding relationships.
I have two tables:
students:
id
student_number
name
etc.......
assessments:
id,
student_id,
criteria 1,
criteria 2,
etc.......
I am using the model to create the relationship:
Model - Student:
public function assessments() {
return $this->hasMany(Assessment::class);
}
Model - Assessment:
public function student(){
return $this->belongsTo(Student::class);
}
This works well, however I would like to add a relationship from Student to a third table. The difficulty is that the third table exists already.
Table
assessment2:
id,
student,
name,
criteria 1,
criteria 2,
etc.......
assessment2.student and students.student_number are the same values.
In the models I have tried the following without success:
Model - Student:
public function assessments2() {
return $this->hasMany(Assessment2::class, 'student');
}
Model - Assessment2:
public function student(){
return $this->belongsTo(Student::class, 'student_number');
}
any ideas appreciated.
Thanks James.
Level 61
Please or to participate in this conversation.