无法从 hasOne 对象访问值 laravel

问题描述 投票:0回答:0

我无法访问 hasOne 返回的值,即使它不为空。

这是我获取数据的代码

$data = StudentGradeCompletionRequest::with(['subject', 'school_year', 'course', 'batch', 'student', 'pendingTag'])
            ->where('payment_status', 2)
            ->where('added_as_fee_status', 1)
            //->where('tag_status', 0)
            ->get();

这是我的 hasOne 关系(内部调用 with(['...', 'pendingTag']) here

我能够得到这样的值 enter image description here

但是当我试图通过像这样遍历 $data 变量来访问它时。

foreach($data as $d){
                    
                    $datas['sgcr_id']       = $d->id;
                    $datas['batch_id']      = $d->batch->id;
                    $datas['student_id']    = $d->student->full_generated_id;
                    $datas['sy_id']         = $d->school_year->school_year;
                    $datas['yr_level']      = $d->year_level;
                    $datas['course']        = $d->course->code;
                    $datas['sem']           = $d->semester;
                    $datas['subject']       = Api::getSubjectTitleCode($d->subject);
                    $datas['student']       = $d->student_fullname;
                    $date                   = explode(',', $d->formatted_request_timestamp);
                    $datas['date']          = $date[1].', '.$date[2];
                    $datas['tag']           = $d->pending_tag;

                    array_push($n_data, $datas);
        }

我在标签中得到这个空值: enter image description here

php laravel
© www.soinside.com 2019 - 2024. All rights reserved.