1

Actually I am facing a problem. I need to send a notification to the admin whenever any update happens in any modal, in any user, and in any table.

To clarify more :

I have a client, a company, and an employee. Under each part, I have many tables ( e.g: under client I have client's profile table, under company I have many tables corresponding to company's lawyer, company's files, etc.., similarly to the employee)

So correspondingly , I have many controllers thus many update methods.

So In order to send a notification to the admin whenever any update happens in any situation under any user. It's too annoying that I must include the notify statement $admin->notify(new UpdateIsMade()); under ALL update METHODS! It's kind of redundancy.

What I want to actually approach is to find a solution that let me automatically send a notification to the admin whenever any update happens by any user under any table.

I tried to make a middleware function and tried to send a notification whenever any update happens under the client user (kind of sampling) :

  public function handle(Request $request, Closure $next)
    {

        $clients = \App\Client::all();

        foreach ($clients as $client) {

          if($client->user->wasChanged()){

           $arr_of_changes = $client->user->getChanges();

           $admin=User::where('profile_type' , 'App\Admin')->first();
           $admin->notify(new UpdateIsMade($arr_of_changes,$original_arr));

           }

        }

        return $next($request);
    }

Unfortunately this middleware didn't work. I guess the problem is that $client->user->wasChanged() is not giving a true value whenever an update happens. It looks like wasChanged only works after the save process in the update method.

Any suggestion for my problem? Thank you for your time and sorry for the long text!

TAS
  • 11
  • 2

1 Answers1

0

It might be less of a headache if you consider using database triggers so monitoring changes is completely separate.

You could create a new table called "change_log" with fields "action" and "created_at" then set up the trigger to insert a new record upon update of any tables you want to monitor.

Then you just set up a CRON to run at intervals that would call a Laravel controller to query the change_log and see if there are any records in it. If there are, the code would take the appropriate action to send an alert then empty the table. The change_log would remain empty until the DB trigger fires due to changes made.

Aaron Belchamber
  • 1,480
  • 1
  • 16
  • 20
  • Alright. I will try this approach thanks! Do you have any idea about laravel events and observers? Do they help me in my approach? – TAS Aug 27 '21 at 15:36
  • I definitely think that is another feasible approach! https://stackoverflow.com/questions/53298322/events-vs-observers-in-laravel After all, an event is a type of trigger. That said, I personally favor the separation above which leverages the native DB capabilities and since it stays separate, it's so simple to implement, and won't need to be involved with the actual change events happening in the Laravel code but I do think both approaches would have their merits concerning being able to update and maintain later -- like if you need to add smarter conditions for event changes perhaps. – Aaron Belchamber Aug 27 '21 at 16:28