Home > other >  Triggering Laravel Notification from external source
Triggering Laravel Notification from external source

Time:06-08

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?

CodePudding user response:

  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}")
  • Related