S.M_Emamian
S.M_Emamian

Reputation: 17383

call a function after a column is changed

I would like to know when a column in my table is changed.

for example, if an user changed fist-name column , I would like to find out it (to call my custom function).

is there any function/library/plugin in laravel to help me ?

Upvotes: 0

Views: 79

Answers (1)

Joel Hinz
Joel Hinz

Reputation: 25374

You could hook into the model's updating event, check if the first name is being changed, and send an event. For instance:

<?php

namespace App\Providers;

use App\User;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        User::updating(function ($user) {
            if ($user->isDirty('first_name')) {
                event(FirstNameWasChanged::class);
            }
        });
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Then you just have to write your notification logic in the event.

Upvotes: 1

Related Questions