-1

enter image description here

How to sum of duration column where my course id or section id is same

2 Answers 2

0

You have to use hasMany relation on this table with course and section table. For hasMany relation see this docs. Here

Then, you can use foreach to generate the sum.

$summation = $yourtablenames->sum(function ($yourtablename) {
    return $yourtablename->your_relation_name->sum('duration');
});
Sign up to request clarification or add additional context in comments.

4 Comments

what is your $yourtablenames and what is $yourtablename. my engine can not find $yourtablenames.
that is your database table or model name based on how you get data from database
You're not even giving us your table/model's name, so people just use other naming for the variable. It's just an usual variable. @AlShahriarMehedi
my model name is (App\Models\Lesson)Lesson and table name is lessons.Now how can I calculate and print this.Please share...
0

Try this code:

$res = Model::groupBy('course_id', 'section_id')
   ->selectRaw('sum(duration) as total_duration, course_id, section_id') // do the sum query here
   ->pluck('total_duration','course_id', 'section_id'); // get the related query column here

Source answer

2 Comments

When I try to print this $res then I got an array. How can I print this sum by using dd?
Inside that array, it has the property named total_duration. That is what you're looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.