2014-12-05 56 views
1

我想總結主題表中的numofunittotal price。我已經做了我被告知在谷歌上做的事情,但是我仍然遇到了一些模糊的錯誤。我想知道我是否這樣做是正確的,因爲我在laravel中查詢db還是個新手。老實說,我不太清楚的語法laravel總結函數

這是查詢的一個片段:

$results = DB::table('subjects') 
      ->join('subjectblocking', 'subjects.subjectcode', '=', 'subjectblocking.subjectcode') 
      ->join('grades', 'subjectblocking.blockcode', '=', 'grades.blockcode') 
      ->select('subjects.numofunit as total_units, subjects.price as total_tuition') 
      ->sum('subjects.numofunit','subjects.price') 
      ->orWhere(function($query) 
      { 
       $query->where('grades.studentid', '=', '2013-F0218') 
         ->where('sem', '=', '1') 
         ->where('sy', '=', '2013-2014'); 
      }) 
      ->get(); 

這是我遇到的錯誤:

調用一個成員函數orWhere()非對象 在線72上:其爲 - > orWhere(函數($查詢)

相信我聲明$query,所以我不知道爲什麼我仍然收到錯誤

回答

3

sum需要去所有的where子句。所以只需將它移動到orWhere之後,刪除多餘的get,並且它應該工作...根據其餘查詢格式良好,但至少在初始階段它看起來不錯。

1

正如Joel Hinz所說,總結需要遵循所有where子句,但是當他說它應該在get()之前去時,他是不正確的。它應該替換get()。

$results = DB::table('subjects') 
     ->join('subjectblocking', 'subjects.subjectcode', '=', 'subjectblocking.subjectcode') 
     ->join('grades', 'subjectblocking.blockcode', '=', 'grades.blockcode') 
     ->select('subjects.numofunit as total_units, subjects.price as total_tuition') 
     ->orWhere(function($query) 
     { 
      $query->where('grades.studentid', '=', '2013-F0218') 
        ->where('sem', '=', '1') 
        ->where('sy', '=', '2013-2014'); 
     }) 
     ->sum('subjects.numofunit','subjects.price'); 
+0

糟糕,你說得對。我會相應地改變我的評論。 – 2014-12-05 21:59:05

+0

謝謝@ColinSchoen @JoelHinz!我現在得到語法,但是在我移動了* - > sum('subjects.numofunit','subjects.price')之後; *它導致了我的返回視圖的錯誤。 – 2014-12-06 07:49:13