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

carcleo's avatar

Get the primary key from table using belongsTo

I hav a following class code:

Student

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    use HasFactory;  
    protected $fillable = [
        'name',
        'email',
        'academic_registration',
        'created_at',
        'updated_at'
    ];
    protected $table = 'students';    
}

StudentDisciplineClassroom (The Pivot table)

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class StudentDisciplineClassroom extends Model
{
    use HasFactory;
    protected $fillable = [
        'student',
        'ra',
        'discipline',
        'classroom',
        'created_at',
        'updated_at'
    ];
    protected $table = 'students_disciplines_classrooms';     

    public function studentRelation(): BelongsTo
    {
        return $this->belongsTo(Student::class, 'ra', 'academic_registration');
    }          
	
	...

}

I need to do this works

public function studentRelation(): BelongsTo {

   return $this->belongsTo(Student::class, 'ra', 'academic_registration');

}          

The problem:

'academic_registration' in Student::class is not the primary key of table and i need get the prymary key (id) this

Then i tryed

$all = $sdc::select('ra')->with('studentRelation')->where('classroom',$request->classroom)->distinct('student')->get();

but studentRelation just returns null

0 likes
11 replies
carcleo's avatar

then, i dont knoe what happened cause the is in post, but only when edit

carcleo's avatar

ok, i am looking for as insert as code in this post, cause it is my first post here

tykus's avatar

@carcleo wrap your code snippets within tripe backticks (```), e.g.

```
// yout code
```
tykus's avatar

There are a lot of questions...

  • What are academic_registration and ra on Student and StudentDisciplineClassroom respectively?
  • Why do you try to make the relationship using anything except the foreign and primary keys?
  • Why do you not follow Eloquent's conventions for naming foreign keys?
  • If students_disciplines_classrooms is a pivot table, what is the other Model, and what is the foreign key associating that table/Model?
carcleo's avatar

I am startng in the frsmework, o go learning the rules with with the time.

I dont change nothing in the database, its long date.

the field academic_ registration ( register of student in the school), is the ra field in the table (Model) Pivot (StudentDisciplineClassroom), and i need get the id of Student at table of students

disciplines and classrooms, but this works fine

carcleo's avatar

@tykus i resolved change the struture of pivot table to only ids from foreighkeys, its solved

tykus's avatar

@carcleo good; best to stay with conventions whenever and wherever possible.

Please or to participate in this conversation.