2017-05-17 7 views
0

ビューまたはブレード内に学校名とその生徒数を表示するのが難しいです。私はschoolsstudentsの2つのテーブルを持っています 私はその学校からの学生の数を表示する必要があります。 学校テーブル:school_idschool_namelatlng 学生テーブル:STUDENT_ID、FNAME、LNAME、BDAY、私は雄弁hasMany関係ビュー内のグループ名とカウントを表示

を使用して、この

[name of school1] : [count of students] 
[name of school2] : [count of students] 
[name of school3] : [count of students] 
[name of school4] : [count of students] 

のようなものを表示したい

をschool_id

どんな助力も高く評価されます。お使いのコントローラでよく

+0

のようにそれを試してみますか? http://stackoverflow.com/help/how-to-askを見てください。 – PVitt

答えて

0

あなたはこのような何か行うことができます。

@foreach($schools as $school) 
    School: {{$school->name}} : {{$school->students()->get()->count()}} Students. 
@endforeach 

うまくいけば、これは動作するはずです:あなたがこれを行うことができ、あなたのビューで

public function index() { 
    $schools = School::all() 

    return view ('schools.index', compact('schools'));  //ofc change the names of the views and such as you like 
} 

、一度に。

EDIT

カウント部は動作しなかった場合は、これまでに試してみました何この

{{$school->count('students')}} 
関連する問題