Farshad
Farshad

Reputation: 2000

watch database and wait for changes in laravel

i have a messaging system app and every message has some status in it with 3 types of done , in progress,not started so when i send a message to user B its not started for example so when user B receive the message he changes the status to in progress here i want to send a notification to the sender and the admin that the status has been changed how can i monitor database for changes and when it changes it generates some specific notification and send it . here is the migration of message table

 public function up()
{
    Schema::create('messages', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title')->nullable();
        $table->string('body');
        $table->integer('status');
        $table->integer('sender_id');
        $table->integer('receiver_id');
        $table->timestamps();
    });
}

Upvotes: 4

Views: 8262

Answers (2)

Yur Gasparyan
Yur Gasparyan

Reputation: 714

Please check https://laravel.com/docs/5.7/eloquent#observers There are some events which you need (Update,Updated,Saved)

Upvotes: 3

D. Petrov
D. Petrov

Reputation: 1167

You can initiate a Message model class to represent the records in your messages table and then listen for the updating or updated event on that model, which fires everytime a record has been updated or is currently updating. There you can check if the updated property is the needed one (in your case - status) and execute the code you desire. I suggest you check the documentation on how to work with eloquent models (if you haven't yet) and look up the provided events. That should do the work. Here is the documentation

Upvotes: 2

Related Questions