jeudi 26 août 2021

How to Fix: Trying to get property 'passing_grade' of non-object Laravel

I have a data course that has been successfully completed by the learner. Then, I want to display the course data and add a certificate download button.

I have code like this, but I get error "Trying to get property 'passing_grade' of non-object".

Controller

    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++;
                    }
                }
            }
            $abc = $progress;
            
            // 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'));
    }

Blade View

@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="" class="img-cover-list">
                                <div class="course-lang type-paragraph2" >
                                    @if($item->course_lang == 1)
                                    <img class="rounded-circle" src="" alt="ID">
                                    @else
                                    <img class="rounded-circle" src="" 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">&#8226 </span>
                                    </div>
                                    <div class="learner-course-info" style="margin-top: 0px;">
                                        
                                    </div>
                                    <div class="type-paragraph3" style="color: #7cdb4a;">
                                        Completion Date : 
                                    </div>
                                </div>
                            </div>
                            <div class="col-2 course-list">
                                "
                                    data-id=""
                                    data-coursename=""
                                    data-date=""
                                    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-"
                                            data-id=""
                                            data-coursename=""
                                            data-date=""
                                            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-"
                                            data-id=""
                                            data-coursename=""
                                            data-date=""
                                            class="dropdown-item certificate-attainment">
                                        Certificate of Attainment
                                        </a>
                                        @endif
                                    </div>
                                    
                                    <div class="modal fade completion-cert-modal-" 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">&times;</span>
                                                    </button>
                                                </div>
                                                <div class="modal-body">
                                                    <div class="embed-responsive embed-responsive-16by9">
                                                        <iframe class="cert-preview embed-responsive-item" src=""></iframe>
                                                    </div>
                                                    <label class="pt-4">CERT ID:</label>
                                                    <div class="input-group">
                                                        <input type="text" class="form-control" id="cert_id_" placeholder="your CERT ID" aria-label="your CERT ID" aria-describedby="basic-addon2" value="" 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_')"> Copy! </button>
                                                        </div>
                                                    </div>
                                                    <label class="pt-2">Validation URL:</label>
                                                    <div class="input-group">
                                                        <input type="text" class="form-control" id="cert_url_" placeholder="your CERT ID" aria-label="your CERT ID" aria-describedby="basic-addon2" value="" 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_')"> Copy! </button>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                                    <a href="" class="btn btn-primary" target="_blank">Download</a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    @if ($item->course->passing_grade != 'N/A' && $item->course->score >= $item->course->passing_grade) 
                                    <!-- Modal Preview Certificate of Attainment -->
                                    <div class="modal fade attainment-cert-modal-" 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">&times;</span>
                                                    </button>
                                                </div>
                                                <div class="modal-body text-left">
                                                    <div class="embed-responsive embed-responsive-16by9">
                                                        <iframe class="cert-preview embed-responsive-item" src=""></iframe>
                                                    </div>
                                                    <label class="pt-4">CERT ID:</label>
                                                    <div class="input-group">
                                                        <input type="text" class="form-control" id="cert_id_" placeholder="your CERT ID" aria-label="your CERT ID" aria-describedby="basic-addon2" value="" 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_')"> Copy! </button>
                                                        </div>
                                                    </div>
                                                    <label class="pt-2">Validation URL:</label>
                                                    <div class="input-group">
                                                        <input type="text" class="form-control" id="cert_url_" placeholder="your CERT ID" aria-label="your CERT ID" aria-describedby="basic-addon2" value="" 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_')"> Copy! </button>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                                    <a href="" class="btn btn-primary" target="_blank">Download</a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <!-- End Modal Preview Certificate of Attainment -->
                                    @endif
                                </div>
                            </div>
                            <div class="col-2 course-list">
                                <a href=""
                                    class="btn btn-secondary-outline">See Detail</a>
                            </div>
                        </div>
                    </li>
                    @php $completed++; @endphp
                    @endif
                    @endif
                    @endif
                    @endforeach
                    @else
                    <div class="empty-internal-course">
                        <img src="" 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="" 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

FYI. There should be more than one course data displayed and click download certificate.

How to fix it? Thank you 🙏



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire