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

bufferoverflow's avatar

Relationship withTrashed() returns null

I'm using soft delete for my Student::class model.

I want to retrieve the soft deleted model in my booking log like this:

Booking::class

// Normal relationship works when student is not softdeleted
public function student()
{
    return $this->belongsTo(Student::class);
}

// This doesn't work, returns null
public function studentRecord()
{
    return $this->belongsTo(Student::class)->withTrashed();
 }

// This temp patch method works but i can not use with Eager loading
public function studentRecordTemp()
{
    return Student::withTrashed()->find($this->student_id);
}

I really can not understand why the second one is returning null. The student_id is defined if i dump the Booking model.

Thanks

0 likes
2 replies
bufferoverflow's avatar

Crap, just realized i have to define the column foreign key now.

return $this->belongsTo(Student::class, 'student_id')->withTrashed();

1 like
SilenceBringer's avatar
Level 55

@bufferoverflow try to specify keys

public function studentRecord()
{
    return $this->belongsTo(Student::class, 'student_id')->withTrashed();
 }
1 like

Please or to participate in this conversation.