duankanjian4642 2014-12-05 20:10
浏览 16
已采纳

在laravel中总结功能

I wanted to sum up the numofunit and total price in the subject table. I already did what I was told to do on google, however I still encountered some errors which is vague to me. I wanted to know if I am doing this right, since I am still new in querying db in laravel. I honestly am not sure of the syntax

This is a snippet of the query:

 $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();

This is the error that I have encountered:

Call to a member function orWhere() on a non-object On line 72: which is ->orWhere(function($query)

I believe that I declared $query, so i'm not sure why I'm still receiving errors

  • 写回答

2条回答 默认 最新

  • doupao5296 2014-12-05 21:31
    关注

    The sum needs to go after all of the where clauses. So just move it to after the orWhere, remove the superfluous get, and it should work... according the rest of your query is well formed, but it looks okay at first clance at least.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划