is there any way to get the foreign keys from a laravel model?
I've found that you can get the primary key by using the method getKeyName(); but havn't found any solution to get the foreign keys.
For example class Grades has the following relations:
public function student()
{
return $this->belongsTo(Student::class, 'student_id', 'id');
}
public function subject()
{
return $this->belongsTo(Subject::class, 'subject_id', 'id');
}
and the following migration:
Schema::create('grades', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('student_id');
$table->unsignedBigInteger('subject_id');
$table->float('grade', 3, 1);
$table->dateTime('graded_at');
$table->timestamps();
$table->foreign('student_id')
->references('id')
->on('students');
$table->foreign('subject_id')
->references('id')
->on('subjects');
$table->unique(['student_id', 'subject_id']);
});
The desired result would be something like:
$grades= new Grades();
return $grades->getForeignKeys();
// desired result
// [
// student_id,
// subject_id
// ]
Is there any way to get all the foreign keys without being able to alter the model?