Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

intelioum's avatar

Using Database Events to Post to an API Webhook

Hello Laravel Enthusiasts,

I have a challenge. I am working with an MS SQL-based on-prem application (old school, Desktop APP) that has no web component at all. I want to create a side App using Laravel Event Listener that every time a specific action happens in the DB to send that info to API End Point. I want to use this template for 30 to 40 events. for example, when a task status changed to completed do this and if canceled do that. The whole idea is to create an event-driven Dashboard based on DB transactions in real-time. I would appreciate any thoughts or feedback.

0 likes
1 reply
LaryAI's avatar
Level 58

To achieve this, you can use Laravel's database events and event listeners. Here's a step-by-step solution:

  1. Create an event class for each specific action you want to track. For example, if you want to track when a task status changes to completed, create a TaskCompleted event class.
php artisan make:event TaskCompleted
  1. In the event class, define any data that you want to pass to the API webhook. For example, you might want to pass the task ID and the new status.
class TaskCompleted
{
    public $taskId;
    public $newStatus;

    public function __construct($taskId, $newStatus)
    {
        $this->taskId = $taskId;
        $this->newStatus = $newStatus;
    }
}
  1. Create an event listener for each event. In the listener, you can make the API request to the webhook endpoint. You can use Laravel's HTTP client (Illuminate\Support\Facades\Http) to make the request.
php artisan make:listener SendTaskCompletedWebhook --event=TaskCompleted
  1. In the listener class, implement the handle method to make the API request. You can access the event data using the $event parameter.
use Illuminate\Support\Facades\Http;

class SendTaskCompletedWebhook
{
    public function handle(TaskCompleted $event)
    {
        $taskId = $event->taskId;
        $newStatus = $event->newStatus;

        // Make the API request
        $response = Http::post('https://example.com/webhook', [
            'taskId' => $taskId,
            'newStatus' => $newStatus,
        ]);

        // Handle the response as needed
        if ($response->successful()) {
            // API request was successful
        } else {
            // API request failed
        }
    }
}
  1. Register the event listeners in the EventServiceProvider class. Open the app/Providers/EventServiceProvider.php file and add the event-listener mappings to the $listen property.
protected $listen = [
    TaskCompleted::class => [
        SendTaskCompletedWebhook::class,
    ],
];
  1. Finally, you need to trigger the events whenever the specific actions happen in the database. You can do this in your application's code where the actions are performed. For example, when a task status changes to completed, you can dispatch the TaskCompleted event.
event(new TaskCompleted($taskId, $newStatus));

That's it! Now, whenever the specified actions happen in the database, the corresponding events will be triggered, and the event listeners will make the API requests to the webhook endpoint.

Note: Make sure to configure your database connection in the Laravel configuration file (config/database.php) to connect to your MS SQL-based on-prem application.

Please or to participate in this conversation.