bugfix> php > 投稿
records = Student::with(array('Mark' => function($marks)
    {
    $marks->select(DB::raw('sum(obtain_marks)'))->with(array('Subject' => function($subject){
    $subject->orderby('subject_name')->select(DB::raw('count(subjects.id)','sum(total_marks)','subject_name'));
        }));
    }))->where('id', $id)->get();

なぜこれが実行されないのですか?

mysqlでの動作 選択したcount(subjects.id)、sum(subjects.total_marks)、sum(marks.obtain_marks)、subjects.subject_nameはstudents.id = marks.student_idのinner join join subject.id = marks.subject_idの内部結合マークStudents.id = subject.subject_nameによる20グループ。 mysqlで動作します。

回答 1 件
  • [選択]で、ローカルキーがある列を選択する必要があります

    $records = Student::with(['Mark' => function($marks){
        $marks->select(DB::raw('sum(obtain_marks)'), 'Subject_id' /* same Problem here ->select('subject_id')*/)->with(['Subject' => function($subject){
            $subject->orderby('subject_name')
                    ->select(DB::raw('count(subjects.id)','sum(total_marks)','subject_name'));
        }]);
    }])
    ->where('id', $id)
    ->select('Mark_id') // you must select foreign key column to use relationship
    ->get();
    
    

あなたの答え