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

noblemfd's avatar

How to prevent a final score from exceeding rating limit

In my Laravel-5.8, I have three models:

  1. GoalType

  2. RatingLimit

  3. Goal

     class GoalType extends Model
     {
       protected $table = 'goal_types';
       protected $primaryKey = 'id';
       protected $fillable = [
               'name',
           ];
    
          return $this->hasMany('App\Models\Goal');
       }
     }
    
    class RatingLimit extends Model
    {
        protected $table = 'rating_limits';
        protected $primaryKey = 'id';
        protected $fillable = [
               'max_rating_percent',
               'goal_type_id',
           ];
    
       public function goaltype()
       {
           return $this->belongsTo('App\Models\lGoalType','goal_type_id');
       }
     }
    
    class Goal extends Model
    {
       protected $table = 'goals';
       protected $fillable = [
                   'id',
                   'goal_type_id',
                   'inititial_score',
                    'final_score',
               ]; 
    
       public function goaltype()
       {
           return $this->belongsTo('App\Models\GoalType','goal_type_id');
       }  
    }
    

At setting, the admin set max_rating_percent (it has integer value) RatingLimit for each GoalType

Each GoalType will have one RatingLimit. One GoalType can have Many Goal, but a Goal can only have one goal Type.

Controller

public function index()
{
    $employeeId = Auth::user()->employee_id;
    $goals = Goal::where('employee_id', $employeeId)->get();
    return view('goals.index')->with('goals', $goals);
}

 public function updateWeight(Request $request, $id)
 {

           $goal                           = Goal::find($id);  
    
            $goal->finalscore    = $request->final_score;             
            $goal->save();

            Session::flash('success', 'Comment is Successfully Updated');
            return redirect()->back();                                               
  }

index.blade

        <table class=" table table-bordered table-striped">
            <thead>
                <tr>
                    <th>
                        Goal Type
                    </th>
                    <th>
                        Initial Score
                    </th>
                </tr>
            </thead>
            <tbody>                    
                @foreach($goals as $key => $goal)
                <tr>
                        <td>
                            {{$goal->goaltype->name ?? '' }}
                        </td>    
                        <td>
                            {{$goal->initial_score ?? '' }}
                        </td>  
                        <td>
                                <a class="btn btn-xs btn-warning" data-toggle="modal" data-target="#final_score_emp{{ $goal->id }}" data-original-title="Comment"> 
                                   <i class="fas text-white">Add Final Score</i>
                                </a>
                       </td>
                </tr>
              @endforeach
        </tbody>
          </table>

modal

                        <div class="modal fade" id="final_score_emp{{ $goal->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                <form action="{{route('appraisal.appraisal_mid_year_setups.employee_mid_year_comment',['id'=>$goal->id])}}" method="post" enctype="multipart/form-data" id="review_comment-form">
                                {{ csrf_field() }}
                                <div class="modal-header">
                                    Self-Review Comment
                                </div>
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="control-label">Comment:<span style="color:red;">*</span></label>
                                          <input type="text" name="final_score" placeholder="Enter score" class="form-control comment">
                                    </div>
                                </div>                                 
                                <div class="modal-footer">
                                    <button  type="submit" id="review_comment_btn-submit" class="btn btn-success btn-ok">Save</button>
                                </div>
                                </form>
                                </div>
                            </div>
                        </div>

When the employee clicks Add Score on the index view blade, it displays a modal form. In the modal form is a text input for score.

What I want to achieve is that on keypress or keyup when the user is typing score, the system should check if the score is more than the max_rating_percent for that goal_type_id, it should display a message or alert that you cannot exceed the max_rating_percent. Considering goal_type_id

final_score <= initial_score + (initial_score * max_rating_percent/100)

How do I achieve this?

Thanks

0 likes
2 replies
Snapey's avatar

You're driving your bus:

  • The bus can hold 12 people.
  • At the first stop 4 people get on.
  • At the second stop 8 people get on, and 3 people got off
  • at the third stop 2 people get off and 2 people got on, and,
  • at the forth stop everyone got off.

The question is what color are the bus driver's eyes?

Please or to participate in this conversation.