在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

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.

As Joel Hinz mentioned, sum needs to go after all of the where clauses, but he is incorrect when he says it should go just before get(). It should replace 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');