Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noblemfd's avatar

How to Make Lesson aggregate score not more than course max score

I am developing a web application on Student Course Management using Laravel-5.8

Models

class Lesson extends Model
{
      protected $table = 'Lessons';
      protected $fillable = [
              'lesson_name',
          'course_id',
          'student_id',
              'score_obtained',
          ];

   public function gradelevel()
    {
        return $this->belongsTo('App\Models\Course','course_id');
    }
    public function student()
    {
        return $this->belongsTo('App\Models\Student','student_id');
    }

}

class Course extends Model
{
  protected $table = 'courses';
  protected $fillable = [
              'course_code',
              'course_name',
              'max_score',
          ];
}  

Controller

class LessonController extends Controller
{
    public function create()
    {
      $courses = Course::all();
      $students = Student::all();
      return view('lessons.create')->with('courses', $courses)->with('students', $students);
    }

    public function store(StoreLessonRequest $request)
    {
     try {
            $lesson = Lesson::create([
                'lesson_name'       => $request->lesson_name,
                'course_id'             => $request->course_id,
                'lesson_id'             => $request->lesson_id,
                'score_obtained'            => $request->score_obtained,
            ]);
            Session::flash('success', 'Lesson is created successfully');
            return redirect()->route('lessons.index');
    } catch (Exception $exception) {
            Session::flash('danger', 'Lesson creation failed!');
            return redirect()->route('lessons.index');
    }
 }
}

create.blade

   <form  action="{{route('lessons.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
       {{csrf_field()}}
       <div class="card-body">
        <div class="form-body">
        <div class="row">

          <div class="col-sm-6">
            <div class="form-group">
                <label>Course</label>
                <select class="form-control select2bs4" data-placeholder="Choose Course" tabindex="1" name="course_id" style="width: 100%;">>
                    <option value="">Select Course</option>
                    @if($courses->count() > 0)
                        @foreach($courses as $course)
                            <option value="{{$course->id}}">{{$course->course_name}}</option>
                        @endforeach
                    @endif
                </select>
            </div>
          </div>

          <div class="col-sm-6">
            <div class="form-group">
                <label>Student</label>
                <select class="form-control select2bs4" data-placeholder="Choose Course" tabindex="1" name="student_id" style="width: 100%;">>
                    <option value="">Select Student</option>
                    @if($students->count() > 0)
                        @foreach($students as $student)
                            <option value="{{$student->id}}">{{$student->student_name}}</option>
                        @endforeach
                    @endif
                </select>
            </div>
          </div>
            
          <div class="col-sm-6">
            <div class="form-group">
                <label>Lesson<span style="color:red;">*</span></label>
               <input  type="text" name="lesson_name" placeholder="Enter lesson here" class="form-control" value="{{old('lesson_name')}}">
            </div>
          </div>
                            
          <div class="col-sm-6">
            <div class="form-group">
                <label>Obtained Score<span style="color:red;">*</span></label>
               <input  type="text" name="score_obtained" placeholder="Enter score obtained here" class="form-control" value="{{old('score_obtained')}}">
            </div>
          </div>

       </div>
     </div>
    </div>          
    <!-- /.card-body -->
    <div class="card-footer">
            <button type="submit" id="submit_create" class="btn btn-primary">Score</button>
    </div>           
       
    </form>

Each core have maximum score allocated and this is done at the setting. For each course, students have lesson and scores are obtained for each lesson.

What I want to achieve is that on the lesson create form, what student score_obtained are being entered the application should sum up all the scores (from the lesson table and the just entered one) the student obtained for that particular course including the one in the textbox, if its more that what is in the max_score in the courses table for that particular course the application should display a message and shouldn't allow the user to submit.

How do I achieve this?

Thank you.

0 likes
3 replies
kylemilloy's avatar

It sounds like you want to do something like...

$student = Student::find($id)
$total = $student->lessons()->sum('score_obtained') / $student->lessons->count()

return $total > Course::find($courseId)->max_score
    ? 'total greater than max_score for course'
    : 'the opposite of that'

Or does it need to sum all student scores?

noblemfd's avatar
  1. It needs to sum all the score of a particular student for a particular score.
  2. Am I writing the code before or after the ->save()
kylemilloy's avatar

If you just need total score then it'll be $student->lessons()->sum('score_obtained') which will work after save.

Please or to participate in this conversation.