0

I have notifications that are sent when specific data is entered by a user through a form on a Laravel app.

ie in a controller

// some db entry
// then notifiy
Notification::send($users, new \app\Notifications\TxnAdded($txn));

I also, have a python script that can generate similar entries in the database and I would the same notifications to be sent when that happens.

TL;DR How do I send Laravel notifications when there is a specific change in the DB or from an external python script?

matt
  • 1,817
  • 14
  • 35
  • If you want to watch the db changes, especially made by 3rd party applications, you can do it only with trigger on DB level. On Laravel side you can watch only events made inside Laravel and only if you used ORM, not query builder or RAW queries. So its impossible. In general, I would recommend to make a hook on Laravel side (some API endpoint) and send there notification from the other services about the changes. – Sergei Shitikov Jun 07 '22 at 12:50

1 Answers1

1
  1. Create command which will trigger event

This command will accept json data as arguments. If there is a lot of data, you may prepare json file and pass a path to it.

<?php
 
namespace App\Console\Commands;
 
use Illuminate\Console\Command;
 
class SendNotification extends Command
{
    protected $signature = 'notification:send {users} {txn}';
 
    protected $description = 'Send a notification to a user';
 
    public function handle()
    {
        // Prepare your data here
        $users = json_decode($this->argument('users'));
        $txn = json_decode($this->argument('txn'));

        Notification::send($users, new \app\Notifications\TxnAdded($txn));
    }
}

Another solution is to create a controller and in the same way call it over http.

  1. Call this command from your python script
os.system("cd /var/www/project && /usr/bin/php artisan notification:send {users as json here} {txn as json here}")
SiZE
  • 2,217
  • 1
  • 13
  • 24