title: Laravel - How to Make Lesson aggregate score not more than course max score
body: <p>I am developing a web application  on Student Course Management using Laravel-5.8</p>
  
  <p>Models</p>
  
  <pre><code>class Lesson extends Model
  {
        protected $table = 'Lessons';
        protected $fillable = [
                'lesson_name',
            'course_id',
            'student_id',
                'score_obtained',
            ];
  
     public function gradelevel()
      {
          return $this-&gt;belongsTo('App\Models\Course','course_id');
      }
      public function student()
      {
          return $this-&gt;belongsTo('App\Models\Student','student_id');
      }
  
  }
  
  class Course extends Model
  {
    protected $table = 'courses';
    protected $fillable = [
                'course_code',
                'course_name',
                'max_score',
            ];
  }  
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>class LessonController extends Controller
  {
      public function create()
      {
        $courses = Course::all();
        $students = Student::all();
        return view('lessons.create')-&gt;with('courses', $courses)-&gt;with('students', $students);
      }
  
      public function store(StoreLessonRequest $request)
      {
       try {
              $lesson = Lesson::create([
                  'lesson_name'       =&gt; $request-&gt;lesson_name,
                  'course_id'             =&gt; $request-&gt;course_id,
                  'lesson_id'             =&gt; $request-&gt;lesson_id,
                  'score_obtained'            =&gt; $request-&gt;score_obtained,
              ]);
              Session::flash('success', 'Lesson is created successfully');
              return redirect()-&gt;route('lessons.index');
      } catch (Exception $exception) {
              Session::flash('danger', 'Lesson creation failed!');
              return redirect()-&gt;route('lessons.index');
      }
   }
  }
  </code></pre>
  
  <p>create.blade</p>
  
  <pre><code>   &lt;form  action="{{route('lessons.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data"&gt;
         {{csrf_field()}}
         &lt;div class="card-body"&gt;
          &lt;div class="form-body"&gt;
          &lt;div class="row"&gt;
  
            &lt;div class="col-sm-6"&gt;
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Course&lt;/label&gt;
                  &lt;select class="form-control select2bs4" data-placeholder="Choose Course" tabindex="1" name="course_id" style="width: 100%;"&gt;&gt;
                      &lt;option value=""&gt;Select Course&lt;/option&gt;
                      @if($courses-&gt;count() &gt; 0)
                          @foreach($courses as $course)
                              &lt;option value="{{$course-&gt;id}}"&gt;{{$course-&gt;course_name}}&lt;/option&gt;
                          @endforeach
                      @endif
                  &lt;/select&gt;
              &lt;/div&gt;
            &lt;/div&gt;
  
            &lt;div class="col-sm-6"&gt;
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Student&lt;/label&gt;
                  &lt;select class="form-control select2bs4" data-placeholder="Choose Course" tabindex="1" name="student_id" style="width: 100%;"&gt;&gt;
                      &lt;option value=""&gt;Select Student&lt;/option&gt;
                      @if($students-&gt;count() &gt; 0)
                          @foreach($students as $student)
                              &lt;option value="{{$student-&gt;id}}"&gt;{{$student-&gt;student_name}}&lt;/option&gt;
                          @endforeach
                      @endif
                  &lt;/select&gt;
              &lt;/div&gt;
            &lt;/div&gt;
  
            &lt;div class="col-sm-6"&gt;
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Lesson&lt;span style="color:red;"&gt;*&lt;/span&gt;&lt;/label&gt;
                 &lt;input  type="text" name="lesson_name" placeholder="Enter lesson here" class="form-control" value="{{old('lesson_name')}}"&gt;
              &lt;/div&gt;
            &lt;/div&gt;
  
            &lt;div class="col-sm-6"&gt;
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Obtained Score&lt;span style="color:red;"&gt;*&lt;/span&gt;&lt;/label&gt;
                 &lt;input  type="text" name="score_obtained" placeholder="Enter score obtained here" class="form-control" value="{{old('score_obtained')}}"&gt;
              &lt;/div&gt;
            &lt;/div&gt;
  
         &lt;/div&gt;
       &lt;/div&gt;
      &lt;/div&gt;          
      &lt;!-- /.card-body --&gt;
      &lt;div class="card-footer"&gt;
              &lt;button type="submit" id="submit_create" class="btn btn-primary"&gt;Score&lt;/button&gt;
      &lt;/div&gt;           
  
      &lt;/form&gt;
  </code></pre>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>How do I achieve this?</p>
  
  <p>Thank you.</p>