我有三个不同的表:persons
,students
和teachers
。我能够连接三个表并获得如下数据:
public function viewProduct()
{
$persons = Person::with(['students','teachers'])->get();
return view('master.viewPeoples', compact('persons'));
}
我想访问刀片中的数据。如何在我的刀片模板中获取学生和教师表的数据?我在rollNum
表格中列year
,semester
和students
以及salary
中的列courses
和teachers
} table。
以下源代码仅返回person
表中的数据。
@foreach($persons as $person)
{{ $person->name }}
{{ $person->universityNum }}
{{ $person->rollNum }} //returns blank
{{ $person->year }} //returns blank
...
@endforeach
当我这样做时,我会收到学生和老师的所有数据:
@foreach($persons as $person)
{{ $person->students }}
{{ $person->teachers }}
@endforeach
但我想分别像$students->rollNum
答案 0 :(得分:1)
试试这个
@foreach($persons as $person)
{{ $person->name }}
@foreach($person->students as $student)
{{ $student->rollNum }}
{{ $student->year }}
@endforeach
// Do same for teachers here
@endforeach
希望有所帮助
答案 1 :(得分:0)
试试这个
@foreach($persons as $person)
{{ $person->students->rollNum }}
{{ $person->teachers->rollNum }}
@endforeach
或
@foreach($persons as $person)
{{ $person->students_rollNum }}
{{ $person->teachers_rollNum }}
@endforeach