以下是我的代码的一部分。但是当我运行该代码时,我收到错误"试图获得非对象的属性"。
$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);
}
}
}
我将如何解决这个问题?
$attendance = DB::table($wys_attendence_table)->where('adate','=',$searchdate)->get();
答案 0 :(得分:5)
问题在这里:
$student = WysStudent::where('studcls',$id)->get();
…
foreach ($student as $student) {
要修复它,请对学生数组使用$studentS
变量,对循环迭代器使用$student
,如下所示:
$students = WysStudent::where('studcls',$id)->get();
…
foreach ($students as $student) {