我很难在视图或刀片中显示学校名称及其学生人数。我有2个表ls | grep -P '\d[eh]\.\w{1,3}'
和schools
我必须显示该学校的学生人数。
学校表:students
,school_id
,school_name
,lat
学生表:student_id,fname,lname,bday,school_id
我想要显示类似这样的内容
lng
使用雄辩的[name of school1] : [count of students]
[name of school2] : [count of students]
[name of school3] : [count of students]
[name of school4] : [count of students]
关系
任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
在您的控制器中,您可以执行以下操作:
public function index() {
$schools = School::all()
return view ('schools.index', compact('schools')); //ofc change the names of the views and such as you like
}
在您看来,您可以这样做:
@foreach($schools as $school)
School: {{$school->name}} : {{$school->students()->get()->count()}} Students.
@endforeach
希望这应该有用。
修改强>
如果计数部分不起作用,请尝试这样
{{$school->count('students')}}