Hello. I am having some trouble getting some data to appear due to a relationship I don't quite have right yet. If I put an not null condition, the page works but I don't get the data that exists in the database but if I don't put in a not null condition, I get the following error:
Trying to access array offset on value of type null
If anyone can help me out in understanding why this relationship isn't working, I would sure appreciate it. Thank you in advance.
Here is the table data in the view:
<tbody>
@foreach($attendances as $attendance)
<tr>
<td>{{ ($attendance->date !=Null) ? date('m-d-Y', strtotime($attendance->date)) : "No data Yet." }}</td>
{{-- <td>{{ $attendance['getCompanyRelation']['name'] }}</td> --}}
<td>{{ ($attendance->teacher_id !=Null) ? $attendance['getTeacherRelation']['name'] : "No Teacher Listed" }}</td>
<td>{{ $attendance->student->name }}</td>
<td>{{ $attendance->attend_status }}</td>
<td>
<a href="{{ route('admin.pages.attendance.attendance_edit', $attendance->id) }}" class="btn btn-primary">Edit</a>
<a href="{{ route('admin.pages.attendance.attendance_delete', $attendance->id) }}" class="btn btn-danger">Delete</a>
</td>
</tr>
@endforeach
</tbody
Here is the Controller for the view:
public function AttendanceReportView() {
$data['attendances'] = Attendance::with('getTeacherRelation', 'getCompanyRelation', 'student')->orderBy('date', 'DESC')->get();
// dd($data['attendances']);
return view('admin.pages.attendance.report_view', $data);
}
And the model I am using:
class Attendance extends Model
{
public function companyRelation() {
return $this->belongsTo(Company::class, 'company_id', 'id');
}
public function student() {
return $this->belongsTo(User::class, 'user_id', 'id');
}
public function attendance() {
return $this->belongsTo(User::class, 'teacher_id', 'id');
}
public static function getAttendances() {
$records = DB::table('attendances')->select('id', 'date', 'attend_status', 'teacher_id', 'user_id')->get()->toArray();
return $records;
}
public function getTeacherRelation() {
return $this->belongsTo(User::class, 'teacher_id', 'id');
}
public function getCompanyRelation() {
return $this->belongsTo(Company::class, 'company_id', 'id');
}
}
Again. Thank you in advance.