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

noblemfd's avatar

How to compare sum of employee score to max_score in setting before submit

I have these two tables in my Laravel-5.8 project

CREATE TABLE `goal_types` (
  `id` int(11) NOT NULL,
  `name` varchar(200) NOT NULL,
  `max_score` int(11) DEFAULT 0,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


CREATE TABLE `employee_goals` (
  `id` bigint(20) NOT NULL,
  `goal_type_id` int(11) DEFAULT NULL,
  `is_published` tinyint(1) DEFAULT 0,
  `employee_id` int(11) DEFAULT NULL,
  `employee_score` int(11) DEFAULT 0,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

max_score is alloted to each goal type and name field in goal type is unique.

However, each employee can have scores for more than one goal types

Model

class GoalType extends Model
{

    protected $table = 'goal_types';

    protected $primaryKey = 'id';

    protected $fillable = [
              'name',
              'max_score',
          ];

    protected $dates = [];

    public function employeegoals()
    {
       return $this->hasMany('App\Models\EmployeeGoal');
    }
}


class EmployeeGoal extends Model
{
    protected $table = 'employee_goals';

    protected $fillable = [
              'id',
              'goal_type_id',
              'employee_id',
              'is_published',
              'employee_score',
          ]; 


    public function goaltype()
    {
        return $this->belongsTo('App\Models\GoalType','goal_type_id');
    }

    public function employee()
    {
        return $this->belongsTo('App\Models\HrEmployee','employee_id');
    }
}

Controller

public function publish_all_posts(){
  $userCompany = Auth::user()->company_id;
  $userEmployee = Auth::user()->employee_id; 

  $goaltypeid;
  $goalmaxscore = DB::table('goal_types')->select('max_score')->where('company_id', $userCompany)->pluck('max_score');
  $employeescore = 0;
  $employeescore = DB::table('employee_goals')->select(DB::raw("IFNULL(SUM(employee_score),0) as employee_score"))->where('employee_id', $userEmployee)->where('goal_type_id', $goaltypeid)->get();  

$unapproved_count = EmployeeGoal::where('employee_id', $userEmployee)->where('is_published',0)->count();

if ($unapproved_count > 3 && $employeescore == $goalmaxscore){
    $unapproved_post = EmployeeGoal::where('employee_id', $userEmployee)->where('is_published',0)
            ->update([
                'is_published' => 1,
                ]);
}

}

I want to set two (2) conditions before it allows submit of the controller function

public function publish_all_posts()

I have successfully set the first condition which is:

if ($unapproved_count > 3)

The second condition is that the application should sum each employee_score in employee_goals where('employee_id', $userEmployee)->where('goal_type_id', $goaltypeid)

$goaltypeid is the value of each id in goal_types table

As I said, I don't have preoblem with $unapproved_count

There are two issues I have:

  1. How do I get the value of $goaltypeid

  2. How do I compare each value $employeescore to $goalmaxscore as in

if ($unapproved_count > 3 && $employeescore == $goalmaxscore){

Thank you

0 likes
0 replies

Please or to participate in this conversation.