1 year ago

Adding Columns of row in Query Builder

Posted 1 year ago by jhutto

I need to create a query to sum items in a row in query builder. Here is my current query in laravel

$SOMASRegistrations = \App\SOMASRegistration::where('CourseID', '=',  $course_id)
        ->join('somascourseregistrationstatus', 'somascourseregistrations.StatusID', '=', 'somascourseregistrationstatus.id')

I need to add three columns within the query and output as another column. Here is the mysql statement that works with the database

SELECT * , (ClassFee + HomeOfficeFee + MaterialsFee ) AS TotalFee FROM somascourseregistrations WHERE 1

I have tried this but it totals all the rows. I need the fields to be total only by each row.

$SOMASRegistrations = DB::table('somascourseregistrations') ->join('somascourseregistrationstatus', 'somascourseregistrations.StatusID', '=', 'somascourseregistrationstatus.id') ->select(DB::raw('sum(ClassFee+HomeOfficeFee+MaterialsFee) AS TotalFee')) ->where('CourseID', $course_id) ->get();

I couldn't find anything that talked about adding columns in a row in laravel Thanks for your help.

Please sign in or create an account to participate in this conversation.