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

rojonunoo's avatar

Can't query pivot table

so i have a pivot table that handles both students and grades i have a Grade model and a student model . so a student hasOne Grade and a Grade hasManny Students. i can query like from grade and get all student like $grade->$students and it gets all students but i can't get get the student's grade in the student profile like $student->grade->grade_name kind of thing does work out don't know why .

public function show($id)
{
    $student=Student::withTrashed()->where('id',$id)->first();
    return view('students.show',compact('student'));

//hear i would like to be able to query and display the students grade }

@foreach($grades->students as $students) contact-img

                                <td>
                                  {{$students->name}}
                                </td>

                                <td>
                                    {{$students->created_at->format('d-m-Y')}}
                                </td>

                                <td>
                                   {{$students->age}}
                                </td>

                                <td>
                                    {{--<a href="#" class="table-action-btn h3"><i class="mdi mdi-pencil-box-outline text-success"></i></a>--}}
                                    <a href="{{url('class/remove-student/' . $grades->id . '/' . $students->id)}}" class="table-action-btn h3"><i class="mdi mdi-close-box-outline text-danger"></i></a>
                                </td>
                            </tr>
@endforeach

thats my view for all students in a particular grade that works fine .

0 likes
8 replies
mrabbani's avatar

Did you add relationship grade method in your Student model?

rojonunoo's avatar

@mrabbani yheaa

class Grade extends Model {

protected $fillable =['grade_name '];

public function User(){
    return $this->belongsTo('App\User');
}

public function students(){
    return $this->belongsToMany('App\Student');
}
RonB1985's avatar

That is your grade model. How about your student model?

He asked: Did you add relationship grade method in your Student model?

rojonunoo's avatar

@RonB1985 @mrabbani o sorry my bad

class Student extends Model { // use SoftDeletes;

/**
 * Accessor for Age.
 */
public function getAgeAttribute()
{
    return Carbon::parse($this->attributes['dob'])->age;
}

protected $dates=['dob','deleted_at'];


protected $fillable=['name','dob','last_school_attended'];

public function guardians(){
    return $this->hasMany('App\Guardian');
}

public function grade(){
    return $this->hasOne('App\Grade');
}
rojonunoo's avatar

@mrabbani gives me this error Call to undefined method Illuminate\Database\Query\Builder::withTrashed()

bcismariu's avatar

do you have grade_id field in our students table?

could you show us your create-students-table migration?

rojonunoo's avatar

@bcismaru no i dont ...

public function up() { Schema::create('students', function (Blueprint $table) {

        $table->engine = 'InnoDB';

        $table->increments('id');
        $table->string('name');
        $table->date('dob');
        $table->string('last_school_attended');
        $table->string('avatar')->default();
        $table->softDeletes('deleted_at');
        $table->timestamps();

Please or to participate in this conversation.