skip to Main Content

Stack:
Laravel v8, MySQL.

I working on a payroll system with Laravel.

I want the value of some columns to update if another column it depends on changes.

For example, if the Number of working days changes on the payrolls table, then the salary and other columns that depends on ‘Number of working days’ column should re-calculate based on the new value and save to the database.

How do I go about this?
Are there any Laravel packages that handles this efficiently?

2

Answers


  1. I never tried it myself, but it seems like you could solve this with a MYSQL Trigger or event:

    https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html

    Login or Signup to reply.
  2. In laravel model, there are events you could use for example if the user first_name or last_name is updated I want to update the full_name too like this

    class User extends Model
    {
        /**
         * The "booted" method of the model.
         *
         * @return void
         */
        protected static function booted()
        {
            static::updating(function ($user) {
                if($user->isDirty('first_name') || $user->isDirty('last_name')){
                     $user->full_name = $user->first_name . ' ' . $user->last_name;
                }
            });
        }
    }
    

    Here is all the events you could listen for: retrieved, creating, created, updating, updated, saving, saved, deleting, deleted, trashed, forceDeleted, restoring, restored, and replicating

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search