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

jaycris's avatar

Accrued Leave Credits

does someone has an idea on how to create a leave credits that will Accrued every month base on the day of Employee hired. like for example when the employee hired on February 24 then on March 24 the leave credits will start to accrued of 1.5 and then on April 24 another 1.5 credits will add.

this is my leave controller by the way

//leave Employee
public function leavesEmployee()
{
    $user = Auth::User()->rec_id;
    $credits = 0;
    $leaves = DB::table('leaves_admin')
                ->where('rec_id',Auth::user()->rec_id)
                ->get();
    return view('Leave.leaveEmployee', compact('leaves','user','credits'));
}

employee or user migration

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('rec_id');
        $table->string('email')->unique();
        $table->string('join_date')->unique();
        $table->string('status')->nullable();
        $table->string('role_name')->nullable();
        $table->string('avatar')->nullable();
        $table->timestamp('email_verified_at')->nullable();
        $table->boolean('needs_password')->default(false);
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

the join_date is the hire date.

0 likes
6 replies
Talinon's avatar

@jaycris

A few things:

What is the point of rec_id on the user table? Can you not just use the primary id? If not, why is the data type for rec_id a string? Why is the join_date a string and not a date type?

In your code, you're accessing rec_id twice unnecessarily. You're assigning it to a temporary variable, then accessing it again in your query.

$user = Auth::User()->rec_id;  // temporary variable
    $credits = 0;
    $leaves = DB::table('leaves_admin')
                ->where('rec_id',$user)     // use temporary variable here instead
                ->get();
    return view('Leave.leaveEmployee', compact('leaves','user','credits'));

As for the credits calculations, there are several techniques you could use. You could set up a daily (nightly) scheduled job that iterates over all the users and calculates their credits and persists it to the database (or perhaps cache the results). This might be the preferred method if your app is going to be generating a lot of on-the-fly queries.

If there is no threat of the business changing how credits are accrued, you could quite easily make an algorithm to determine the credits for a user. Something like:

$credits = Carbon\Carbon::parse($user->join_date)->startOfDay()->diffInMonths(now()) * 1.5;
1 like
jaycris's avatar

@Talinon hi thank you for help in accrued leave credits, i think there is no threat in the business changing how credits are accrued since they only want automatic accrued for all leave credits. unfortunately this code is not mine, it is the work of the previous developer of this company. Upon tracing the code, the use of rec_id is the employee id which is sometimes the company use letters in employee id. I think that's why the dev use string instead of int. And for the join date, i don't know why they use string also.. Anyway, thank you for this help. I will try to implement this.

jaycris's avatar

@Talinon sir what if there is a limit? 15 leave credits, so when they reach 15 it will stop increasing.

Talinon's avatar
Talinon
Best Answer
Level 51

@jaycris

function getCredits($user) {

	$credits = Carbon\Carbon::parse($user->join_date)->startOfDay()->diffInMonths(now()) * 1.5;

	return $credits < 15 ? $credits : 15;  // return actual credits unless 15 or more, than always return 15

}

Sounds like you may need to factor in the "leaves" though.

1 like
Snapey's avatar

what happens at the anniversary of them joining?

Please or to participate in this conversation.