2014-12-05 57 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