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

Fajar's avatar
Level 2

nested eager load with where condition

hey, maybe this question is not much different from my question before the tread which was answered

here I already have data that matches between the tables absensis, students, users

but when I do foreach and call names this way

$student = Absensi::with(['student.user' => function($query){
            $query->where('student_id', Auth::user()->id);
        }])->get();

foreach($student as $student){
            dd($student->student->user->name);
        }

i get error message

property 'user' of non-object

thx for youre answer

0 likes
35 replies
gitwithravish's avatar

If the relation name is siswa then why are you trying to access it with the property student ?

dd($teacher->siswa->user->name);

This error can also occure if there are no students for the given model.

laracoft's avatar

@fajar

Which line is causing the error? Check your stack trace, it will say clearly. Otherwise paste it here.

1: $absensis = Absensi::with(['student.user' => function($query){
2:     $query->where('student_id', Auth::user()->id);
3: }])->get();
4: 
5: foreach($absensis as $absensi){
6:     dd($absensi->student->user->name);
7: }
Fajar's avatar
Level 2

the message leads to

Trying to get property 'user' of non-object

dd($student->student->user->name);

laracoft's avatar

@fajar

$absensis = Absensi::with('student.user')->has('student.user')->whereStudentId(Auth::id())->get();

foreach ($absensis as $absensi) {
    $absensi->student->user->name;
}
Fajar's avatar
Level 2

for attendance table I have 2 nullable fields

student_id
teacher_id

and now I have a row filled in student_id

when I try this code

$absensis = Absensi::with('student.user')->has('student.user')->whereStudentId(Auth::id())->get();

foreach ($absensis as $absensi) {
    $absensi->student->user->name;
}

i get blank white screen

Fajar's avatar
Level 2

I've done it..that I got is blank white screen

Fajar's avatar
Level 2

i get this

Illuminate\Database\Eloquent\Collection {#1294 ▼
  #items: []
}

laracoft's avatar

@fajar

You don't have a student that has the current user ID, Auth::id()

$absensis = Absensi::with('student.user')->has('student.user')->get();

Fajar's avatar
Level 2

I own it

in the user table there is id 4 which, in the student table there is a user_id which contains 4 as my liaison between students and users, in the absensis table I have user_id 4

Fajar's avatar
Level 2

if i try

$absensis = Absensi::with('student.user')->has('student.user')->get();

Illuminate\Database\Eloquent\Collection {#359 ▼ #items: [] }

laracoft's avatar

@fajar

Use dd($absensis->count()); to be more useful. Or remove the dd(...).

laracoft's avatar

@fajar

How about this?

$absensis = Absensi::whereStudentId(Auth::id())->get();
dd($absensis->count());
laracoft's avatar
$absensis = Absensi::whereStudentId(Auth::id())->with('student.user')->get();
dd($absensis->count());

If you get 0 now, then something wrong with your database, do you have another stored procedure?

Fajar's avatar
Level 2

but if i try again with

foreach ($student as $key) {
            dd($key->student->user->name);
        }

get this

Trying to get property 'user' of non-object

laracoft's avatar

@fajar

It is odd that you keep using $student.

$absensis = Absensi::whereStudentId(Auth::id())->with('student.user')->get();
dd($absensis);

Can you fully expand the dd(...) by clicking on all the arrows?

Fajar's avatar
Level 2

if itry this

foreach ($student as $key) {
            dd($key->student->id);
        }

i get this

Trying to get property 'id' of non-object
Fajar's avatar
Level 2

this full code

$student = Absensi::whereStudentId(Auth::id())->with('student.user')->get();

        foreach ($student as $key) {
            dd($key->student->user->name);
        }

laracoft's avatar

@fajar

We have gone through this. You have a absensis.student_id that is NULL.

The error is saying it cannot execute NULL->student->id.

laracoft's avatar
laracoft
Best Answer
Level 27
$student = Absensi::whereStudentId(Auth::id())->with('student.user')->get();

        foreach ($student as $key) {
            dump($key);
        }

Show full output

Fajar's avatar
Level 2

i try again

$student = Absensi::whereStudentId(Auth::id())->with('student.user')->get();

        dd($student);

i get

Illuminate\Database\Eloquent\Collection {#1294 ▼
  #items: array:1 [▼
    0 => App\Absensi {#1292 ▼
      #table: "absensis"
      #fillable: array:4 [▶]
      #connection: "mysql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:6 [▼
        "id" => 1
        "siswa_id" => 4
        "guru_id" => null
        "absen" => "hadir"
        "created_at" => "2021-04-24 10:13:01"
        "updated_at" => "2021-04-24 10:13:01"
      ]
      #original: array:6 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "siswa" => null
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }
  ]
}
Fajar's avatar
Level 2

omg ... I hope that all connected via student_id can connect students and the user_id of students can connect it turns out that the user being read is the id

Fajar's avatar
Level 2

why the student read is id .. why not user_id

laracoft's avatar

@fajar

Do you even have a siswa table?

Your code is too messy. Student become teacher, Absent become student.

Paste direct code, there is no need to translate.

Fajar's avatar
Level 2

of course I have a student table

laracoft's avatar

@fajar

$absensis = Absensi::with(['siswa.user' => function($user){
    $user->where('id', Auth::user()->id);
}])->get();
Fajar's avatar
Level 2

This code explains that what Auth :: user () -> id matches is the id in the student table

$absensis = Absensi::with(['siswa.user' => function($user){
    $user->where('id', Auth::user()->id);
}])->get();

if like this there will be a problem. If later there is a student with id 4 then the name will be different .. example student id 4 is joh, while in the table user id 4 is doe, so the name that will appear in the absence is john

Please or to participate in this conversation.