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.
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
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.
sorry i wrote it wrong
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: }
the message leads to
Trying to get property 'user' of non-object
dd($student->student->user->name);
You have some absensis records with NULL student_id
$absensis = Absensi::with('student.user')->has('student.user')->whereStudentId(Auth::id())->get();
foreach ($absensis as $absensi) {
$absensi->student->user->name;
}
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
dump($absensi->student->user->name);
I've done it..that I got is blank white screen
What if you do dd($absensis);?
i get this
Illuminate\Database\Eloquent\Collection {#1294 ▼
#items: []
}
You don't have a student that has the current user ID, Auth::id()
$absensis = Absensi::with('student.user')->has('student.user')->get();
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
if i try
$absensis = Absensi::with('student.user')->has('student.user')->get();
Illuminate\Database\Eloquent\Collection {#359 ▼ #items: [] }
Use dd($absensis->count()); to be more useful. Or remove the dd(...).
i get
0
How about this?
$absensis = Absensi::whereStudentId(Auth::id())->get();
dd($absensis->count());
i get
1
$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?
i get
1
@fajar
You can remove the dd() now, good luck!
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
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?
if itry this
foreach ($student as $key) {
dd($key->student->id);
}
i get this
Trying to get property 'id' of non-object
this full code
$student = Absensi::whereStudentId(Auth::id())->with('student.user')->get();
foreach ($student as $key) {
dd($key->student->user->name);
}
We have gone through this. You have a absensis.student_id that is NULL.
The error is saying it cannot execute NULL->student->id.
$student = Absensi::whereStudentId(Auth::id())->with('student.user')->get();
foreach ($student as $key) {
dump($key);
}
Show full output
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 [▶]
}
]
}
Your "siswa" => null, that is the cause of your error.
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
why the student read is id .. why not user_id
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.
of course I have a student table
$absensis = Absensi::with(['siswa.user' => function($user){
$user->where('id', Auth::user()->id);
}])->get();
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.