Postingan lainnya
Bagaimana Cara Memperbaiki Error: Trying to get property 'passing_grade' of non-object di Laravel
Saya memiliki data course yang telah berhasil diselesaikan oleh learner. Kemudian, saya ingin menampilkan data course tersebut dan menambahkan tombol download sertifikat dengan dropdown dan modal.
Saya memiliki kode seperti ini, tetapi saya mendapatkan error "Trying to get property 'passing_grade' of non-object".
CourseManagementController.php
public function indexListCompetency()
{
$user = Auth::user('lms_web');
$course_case = DB::raw("course.id, course.title, course.cover_image, course.is_published,
CASE
WHEN COUNT(classroom.id) AND (
COUNT(group_discussion.id) OR
COUNT(competency_file.competency_id) OR
COUNT(course_activity.id)
)
THEN \"Blended\"
WHEN COUNT(classroom.id)
THEN \"Offline\"
ELSE \"Online\"
END AS course_contain");
if ($user->customer_type_id == 4) {
$course_learner = CourseLearner::select($course_case, 'course_learner.*',
DB::raw('SUM(duration) as total_duration,
CASE WHEN course.corporate_id IS NOT NULL THEN "Internal"
ELSE "External" END AS course_provider'
)
);
} else {
$course_learner = CourseLearner::select($course_case, 'course_learner.*',
DB::raw('SUM(duration) as total_duration,
CASE WHEN course.hotel_id IS NOT NULL THEN "Internal"
ELSE "External" END AS course_provider'
)
);
}
$course_taken = $course_learner
->where('learner_id', auth('lms_web')->user()->id)
->join('course', 'course.id', 'course_learner.course_id')
->leftJoin('group_discussion','group_discussion.course_id','course.id')
->leftJoin('classroom','classroom.course_id','course.id')
->leftJoin('course_activity','course_activity.course_id','course.id')
->leftJoin('competency_file','competency_file.competency_id','course.id')
->join('file_assignment', 'file_assignment.id', 'competency_file.file_assignment_id')
->orderByDesc('course_learner.updated_at')
->groupBy('course_learner.id', 'course_learner.course_id', 'course_learner.trainer_id',
'course_learner.batch_id', 'course_learner.learner_id', 'course_learner.open_at', 'course_learner.close_at',
'course_learner.created_at', 'course_learner.updated_at', 'course_learner.is_complete',
'course.id', 'course.title', 'course.cover_image', 'course.is_published');
$progress = Progress::select('course_id', DB::raw('SUM(value) as progress'))
->where('user_id', auth('lms_web')->user()->id)
->groupBy('course_id')
->get();
$user_courses = $course_taken->get();
$complete = 0;
// dd($user_courses);
foreach ($user_courses as $key => $item) {
foreach ($progress as $items) {
if ($item->course_id == $items->course_id) {
if ($item->total_duration <= $items->progress) {
$complete++;
}
}
}
// Certificate of Completion
$cert_comp = new CertificateNumber;
$cert_comp->set_completion($user->id, $item->course_id, $user->def_corporate_id, $user->def_hotel_id);
$cert_comp_num = $cert_comp->getID();
// Certificate of Completion
$cert_att = new CertificateNumber;
$cert_att->set_attainment($user->id, $item->course_id, $user->def_corporate_id, $user->def_hotel_id);
$cert_att_num = $cert_att->getID();
$user_courses[$key]['cert_completion_num'] = $cert_comp_num;
$user_courses[$key]['cert_attainment_num'] = $cert_att_num;
$user_courses[$key]['course'] = $cert_comp->course;
// dd($item->course);
}
switch ($user->customer_type_id) {
case 4:
$company = Corporate::find($user->def_corporate_id);
break;
default:
$company = Hotel::find($user->def_hotel_id);
break;
}
return view('learning.listcompetency.index', compact('cert_comp_num','user', 'user_courses', 'progress', 'complete', 'company'));
}
dan
index.blade.php
@forelse($user_courses as $item)
@if ($item->course_provider == 'External')
@foreach ($progress as $items)
@if($items->course_id == $item->course_id)
@if ($item->total_duration != 0)
@if (round(($items->progress/$item->total_duration)*100) >= 100)
<li class="list-group-item">
<div class="row mycourse-list">
<div class="col-2">
<img src="{{ asset($item->cover_image) }}" class="img-cover-list">
<div class="course-lang type-paragraph2" >
@if($item->course_lang == 1)
<img class="rounded-circle" src="{{ asset('img/icon/id_square.png') }}" alt="ID">
[@else](https://kotakode.com/users/624)
<img class="rounded-circle" src="{{ asset('img/icon/uk_square.png') }}" alt="ENG">
@endif
</div>
<div class="course-status type-paragraph2 published">FREE</div>
</div>
<div class="col-6 course-list">
<div class="course-detail type-paragraph5">
<div class="course-type">
AJAR <span class="course-mode">• {{ $item->course_contain }}</span>
</div>
<div class="learner-course-info" style="margin-top: 0px;">
{{ $item->title }}
</div>
<div class="type-paragraph3" style="color: #7cdb4a;">
Completion Date : {{ Carbon\Carbon::parse($item->updated_at)->format('M d, Y') }}
</div>
</div>
</div>
<div class="col-2 course-list">
{{-- <a href="#" data-toggle="modal" data-target=".completion-cert-modal-{{ $item->course_id }}"
data-id="{{ $item->course_id }}"
data-coursename="{{ $item->course->title }}"
data-date="{{ Carbon\Carbon::parse($item->updated_at)->format('M d, Y') }}"
class="btn btn-secondary-outline certificate">
Certificate
</a> --}}
<div class="dropdown">
<button class="btn btn-secondary-outline dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Certificate
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a href="#" data-toggle="modal" data-target=".completion-cert-modal-{{ $item->id . $item->learner_id . '1' }}"
data-id="{{ $item->id }}"
data-coursename="{{ $item->title }}"
data-date="{{ \Carbon\Carbon::parse($item->updated_at)->format('M d, Y') }}"
class="dropdown-item certificate">
Certificate of Completion
</a>
@if ($item->course->passing_grade != 'N/A' && $item->course->score >= $item->course->passing_grade)
<a href="#" data-toggle="modal" data-target=".attainment-cert-modal-{{ $item->id . $item->learner_id . '2' }}"
data-id="{{ $item->id }}"
data-coursename="{{ $item->title }}"
data-date="{{ \Carbon\Carbon::parse($item->updated_at)->format('M d, Y') }}"
class="dropdown-item certificate-attainment">
Certificate of Attainment
</a>
@endif
</div>
{{-- Modal Preview Certificate of Completion --}}
<div class="modal fade completion-cert-modal-{{ $item->id . $item->learner_id . '1' }}" tabindex="-1" role="dialog" aria-labelledby="completionCertModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Certificate of Completion</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="embed-responsive embed-responsive-16by9">
<iframe class="cert-preview embed-responsive-item" src="{{ route('lms.custom_certificate.preview', ['pfx'=>$pfx, 'id' => $item->cert_completion_num])}}"></iframe>
</div>
<label class="pt-4">CERT ID:</label>
<div class="input-group">
<input type="text" class="form-control" id="cert_id_{{ $item->id . $item->learner_id . '1' }}" placeholder="your CERT ID" aria-label="your CERT ID" aria-describedby="basic-addon2" value="{{ $item->cert_completion_num }}" readonly style="background: #fff" onclick="javascript:$(this).select()">
<div class="input-group-append">
<button class="btn btn-outline-secondary copy-text" style="border-radius: 0 .25rem .25rem 0; padding: 5px 10px !important;" type="button" onclick="copyText('cert_id_{{ $item->id . $item->learner_id . '1' }}')"> Copy! </button>
</div>
</div>
<label class="pt-2">Validation URL:</label>
<div class="input-group">
<input type="text" class="form-control" id="cert_url_{{ $item->id . $item->learner_id . '1' }}" placeholder="your CERT ID" aria-label="your CERT ID" aria-describedby="basic-addon2" value="{{ route('lms.custom_certificate.verified', ['id' => $item->cert_completion_num]) }}" readonly style="background: #fff" onclick="javascript:$(this).select()">
<div class="input-group-append">
<button class="btn btn-outline-secondary copy-text" style="border-radius: 0 .25rem .25rem 0; padding: 5px 10px !important;" type="button" onclick="copyText('cert_url_{{ $item->id . $item->learner_id . '1' }}')"> Copy! </button>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<a href="{{ route("lms.completioncert", ["pfx" => $pfx, "cert_id" => $item->cert_comp_num]) }}" class="btn btn-primary" target="_blank">Download</a>
</div>
</div>
</div>
</div>
{{-- End Modal Preview Certificate of Completion --}}
@if ($item->course->passing_grade != 'N/A' && $item->course->score >= $item->course->passing_grade)
<div class="modal fade attainment-cert-modal-{{ $item->id . $item->learner_id . '2' }}" tabindex="-1" role="dialog" aria-labelledby="attainmentCertModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Certificate of Attainment</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body text-left">
<div class="embed-responsive embed-responsive-16by9">
<iframe class="cert-preview embed-responsive-item" src="{{ route('lms.attainment_certificate.preview', ['pfx'=>$pfx, 'id' => $item->cert_attainment_num]) }}"></iframe>
</div>
<label class="pt-4">CERT ID:</label>
<div class="input-group">
<input type="text" class="form-control" id="cert_id_{{ $item->id . $item->learner_id . '2' }}" placeholder="your CERT ID" aria-label="your CERT ID" aria-describedby="basic-addon2" value="{{ $item->cert_attainment_num }}" readonly style="background: #fff" onclick="javascript:$(this).select()">
<div class="input-group-append">
<button class="btn btn-outline-secondary copy-text" style="border-radius: 0 .25rem .25rem 0; padding: 5px 10px !important;" type="button" onclick="copyText('cert_id_{{ $item->id . $item->learner_id . '2' }}')"> Copy! </button>
</div>
</div>
<label class="pt-2">Validation URL:</label>
<div class="input-group">
<input type="text" class="form-control" id="cert_url_{{ $item->id . $item->learner_id . '2' }}" placeholder="your CERT ID" aria-label="your CERT ID" aria-describedby="basic-addon2" value="{{ route('lms.custom_certificate.verified', ['id' => $item->cert_attainment_num]) }}" readonly style="background: #fff" onclick="javascript:$(this).select()">
<div class="input-group-append">
<button class="btn btn-outline-secondary copy-text" style="border-radius: 0 .25rem .25rem 0; padding: 5px 10px !important;" type="button" onclick="copyText('cert_url_{{ $item->id . $item->learner_id . '2' }}')"> Copy! </button>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<a href="{{ route("lms.attainmentcert", ["pfx" => $pfx, "cert_id" => $item->cert_attainment_num]) }}" class="btn btn-primary" target="_blank">Download</a>
</div>
</div>
</div>
</div>
@endif
</div>
</div>
<div class="col-2 course-list">
<a href="{{ route('lms.courses.overview', ['pfx' => $pfx, 'id' => $item->course_id]) }}"
class="btn btn-secondary-outline">See Detail</a>
</div>
</div>
</li>
@php $completed++; @endphp
@endif
@endif
@endif
@endforeach
[@else](https://kotakode.com/users/624)
<div class="empty-internal-course">
<img src="{{ asset('img/empty-learning-passport.png') }}" class="img-responsive"
style="padding:20px;" alt="Empty learning passport">
<div class="data-empty">
You haven’t completed any courses this time.
</div>
</div>
@break
@endif
@empty
<div class="empty-internal-course">
<img src="{{ asset('img/empty-learning-passport.png') }}" class="img-responsive"
style="padding:20px;" alt="Empty learning passport">
<div class="data-empty">
You haven’t completed any courses this time.
</div>
</div>
@endforelse
Jika saya coba dd($item), hasilnya akan seperti ini
dd(item).jpg
Dan jika saya coba dd($item->course) seperti ini
dd(itemcourse).jpg
FYI. Ada lebih dari satu data course yang seharusnya ditampilkan dan ada dropdown button (download certificate).
Jika download button tidak ditambahkan, data course (yang sudah diselesaikan learner ) akan tampil dengan jumlah yang sesuai, tapi jika ditambahkan download button akan error seperti ini.
Bagaimana cara memperbaiki error seperti ini? Thank you 🙏
2 Jawaban:
<div>apakah semua item pasti punya relasi course?</div>
Tanggapan
iya mas, stiap item, punya relasi dgn course
<div>Coba tambahkan kondisi <br><br></div><pre>@if( null !== $item->course->passing_grade )</pre>