2017-07-11 6 views
-1

ERD: enter image description here雄弁からどのように照会するのですか?

モデル

  1. Student:ID
  2. Course:ID
  3. Student_Course(支払い):ID、STUDENT_ID、Course_IDに

関係との間のとCourseは多対多であるため、別のテーブルを作成しますStudent_Course

質問があります。少なくとも1つのコースを登録する合計学生数を表示してください

私は結果を見つけるために助けてください。私はそれに固執した。

答えて

0

次の例で試してみてくださいすることができます

Model/Student.php //write relationship in your model 

public function courses() 
{ 
    return $this->belongsToMany(Course::class, 'payment'); 
} 

をしてから

$students = Student::whereHas('courses') 
    ->take(10) 
    ->get(); 
+0

HorseTrainingとは何ですか?それはモデルですか? – Axaxaxaxax

+0

@AlvianSupriadi sorry typo、yesそれはコースモデルです –

0

説得力のクエリ以下にしようとはこれを試してみてください。

Illuminate \ Database \ Eloquent \ Modelを使用します。

class Student extends Model { 

    public function student_courses() { 
     return $this->hasMany('App\StudentCourses'); 
    } 
} 


$students = Student::whereHas('student_courses')->get(); 
関連する問題