trailcat's avatar

Logging form updates made by employees

I am building a system which will save data related to work projects created by employees. All information will be entered via forms and stored in DB tables.

One requirement is that there needs to be a mechanism which is triggered nightly that sends an automated email to our supervisor of any changes that have been made to existing projects. Is there a preferred way to do this in Laravel?

0 likes
5 replies
LaryAI's avatar
Level 58

Certainly! Here’s a clear solution, leveraging Laravel’s built-in features:

Step 1: Track Updates with Model Events or Audit Package

You can track project updates using Eloquent model events or a package like spatie/laravel-activitylog.

Option A: Using Model Events

Add a updated_by and updated_at column if you don’t already track who edited the record and when.

In your Project model:

class Project extends Model
{
    protected static function booted()
    {
        static::updating(function ($project) {
            // Save a record of this update in a log table
            ProjectUpdateLog::create([
                'project_id' => $project->id,
                'updated_by' => auth()->id(),
                'updated_at' => now(),
                'changes' => json_encode($project->getDirty()),
            ]);
        });
    }
}

Define a project_update_logs table with columns like:
(id, project_id, updated_by, updated_at, changes)

Step 2: Nightly Email Job

Create an Artisan command or scheduled job.

Command Example:

php artisan make:command EmailProjectUpdates

In app/Console/Commands/EmailProjectUpdates.php:

public function handle()
{
    $since = now()->subDay();
    $updates = ProjectUpdateLog::where('updated_at', '>=', $since)->get();

    if ($updates->isNotEmpty()) {
        Mail::to('[email protected]')->send(new \App\Mail\ProjectUpdatesReport($updates));
    }
}

Step 3: Schedule the Command

In app/Console/Kernel.php:

protected function schedule(Schedule $schedule)
{
    $schedule->command('email:project-updates')->dailyAt('01:00');
}

Step 4: The Mail Class

Generate a mail class and use a markdown blade view to present the updates.

php artisan make:mail ProjectUpdatesReport --markdown=emails.project-updates

In the view, loop through the $updates and display what changed.


Alternate: Use spatie/laravel-activitylog

Install and configure spatie/laravel-activitylog. Log changes to your Project models automatically and query the log table in your command.


Summary

  • Track updates in a log table (manually or with a package)
  • Nightly, run a scheduled command to aggregate updates
  • Send a formatted email to your supervisor

This is robust, scalable, and fully leverages Laravel features. Let me know if you’d like more detailed code for any section!

Glukinho's avatar

Scheduled command + simple model query + mailable, no problem at all.

1 like
martinbean's avatar

One requirement is that there needs to be a mechanism which is triggered nightly that sends an automated email to our supervisor of any changes that have been made to existing projects. Is there a preferred way to do this in Laravel?

@trailcat https://laravel.com/docs/scheduling

Please or to participate in this conversation.