I would suggest the following:
you have three tables students, degrees and consultants
migration looks something like this:
Schema::create('students', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::create('degrees', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::create('consultants', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
the pivot table goes like this:
Schema::create('degree_student', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('consultant_id');
$table->unsignedInteger('degree_id');
$table->unsignedInteger('student_id');
$table->timestamps();
$table->foreign('consultant_id')->references('id')->on('consultants');
$table->foreign('degree_id')->references('id')->on('degrees');
$table->foreign('student_id')->references('id')->on('students');
});
note that the pivot-table is named degree_student. This is because we make a many-to-many-relationship between students and degrees.
The Models then look like this:
class Student extends Model
{
public function degrees()
{
return $this->belongsToMany('App\Degree')
->using('App\DegreeStudent')
->withPivot('consultant_id');
}
}
class Degree extends Model
{
public function students()
{
return $this->belongsToMany('App\Student')
->using('App\DegreeStudent')
->withPivot('consultant_id');
}
}
class Consultant extends Model
{
public function degree_student()
{
return $this->hasMany('App\DegreeStudent');
}
}
And we need an additional Model for the pivot-table:
use Illuminate\Database\Eloquent\Relations\Pivot;
class DegreeStudent extends Pivot
{
public function consultant()
{
return $this->belongsTo('App\Consultant');
}
}
note that this Model extends Pivot instead of Model !
With this you can access the consultant for a student in a specific degree like this:
App\Student::find(1)->degrees()->find(1)->pivot->consultant()->get()
I hope I don't have any typos in here, and I also hope, you get the point with this solution.
[EDIT]
I must admit, that this solution doesn't give you all possibilities. You can't find students and degrees for a given consultant. Maybe you need to make many-to-many-relationships for consultant-student and consultant-degree as well. Good Luck! :)