简体   繁体   English

如何解决laravel中的错误``试图获取非对象的属性''

[英]How to solve error 'Trying to get property of non-object' in laravel

The following is the part of my code. 以下是我的代码的一部分。 But when I run that code, I am getting error "Trying to get property of non-object". 但是,当我运行该代码时,出现错误“试图获取非对象的属性”。

            $searchdate = Input::get('curdate');
            $teacherid = Auth::user()->id;
            $student = WysStudent::where('studcls',$id)->get();
            $clss = WysClass::where('clsteacher_id',$teacherid)->get();
            $attendence_tbl = WysAttendancename::where('cls_id',$id)->first();
            $wys_attendence_table = $attendence_tbl->attendance_name;
            $attendance = DB::table($wys_attendence_table)->where('adate','=',$searchdate)->get();
            if($attendance && $student){
                foreach ($attendance as $attendance) {
                    foreach ($student as $student) {
                        var_dump($student->id);
                    }
                }                
            }

How will I solve this problem?? 我将如何解决这个问题?

$attendance = DB::table($wys_attendence_table)->where('adate','=',$searchdate)->get();

Issue is here: 问题在这里:

$student = WysStudent::where('studcls',$id)->get();
…
foreach ($student as $student) {

To fix it use $studentS variable for array of students and $student for loop iterator, like this: 要解决此问题,请对学生数组使用$studentS变量,对循环迭代器使用$student ,如下所示:

$students = WysStudent::where('studcls',$id)->get();
…
foreach ($students as $student) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM