PHPackages                             huangdijia/laravel-trigger - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. huangdijia/laravel-trigger

ActiveLibrary[Database &amp; ORM](/categories/database)

huangdijia/laravel-trigger
==========================

MySQL trigger base on MySQLReplication.

v6.0.1(2mo ago)325.4k2MITPHPPHP ^8.2CI passing

Since May 30Pushed 2mo ago3 watchersCompare

[ Source](https://github.com/huangdijia/laravel-trigger)[ Packagist](https://packagist.org/packages/huangdijia/laravel-trigger)[ Docs](https://github.com/huangdijia/laravel-trigger)[ Fund](https://hdj.me/sponsors/)[ GitHub Sponsors](https://github.com/huangdijia)[ RSS](/packages/huangdijia-laravel-trigger/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (48)Used By (0)

laravel-trigger
===============

[](#laravel-trigger)

[![Latest Test](https://github.com/huangdijia/laravel-trigger/workflows/tests/badge.svg)](https://github.com/huangdijia/laravel-trigger/actions)[![Latest Stable Version](https://camo.githubusercontent.com/0052e81d11998998dc0b5c882658bc4d5f3802be48fe4f60c2e7b6d4faacfdda/68747470733a2f2f706f7365722e707567782e6f72672f6875616e6764696a69612f6c61726176656c2d747269676765722f76657273696f6e2e706e67)](https://packagist.org/packages/huangdijia/laravel-trigger)[![Total Downloads](https://camo.githubusercontent.com/ea17131303b15c0b4a9b0a960f83e28574660b7d56efc89dd7a2c9415df5deb6/68747470733a2f2f706f7365722e707567782e6f72672f6875616e6764696a69612f6c61726176656c2d747269676765722f642f746f74616c2e706e67)](https://packagist.org/packages/huangdijia/laravel-trigger)[![GitHub license](https://camo.githubusercontent.com/cfd9562c8ad9d6f0064cfe611ef49dd4d2e1716aba8d396d7ddb43bd4112e64b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6875616e6764696a69612f6c61726176656c2d74726967676572)](https://github.com/huangdijia/laravel-trigger)

Subscribe to MySQL events like jQuery, based on [php-mysql-replication](https://github.com/krowinski/php-mysql-replication)

[中文说明](README-CN.md)

Quick Start
-----------

[](#quick-start)

1. Install the package: `composer require "huangdijia/laravel-trigger:^4.0"`
2. Configure your MySQL server for replication (see [MySQL Server Configuration](#mysql-server-configuration))
3. Publish the config: `php artisan vendor:publish --provider="Huangdijia\Trigger\TriggerServiceProvider"`
4. Configure your `.env` file with database credentials
5. Start listening: `php artisan trigger:start`

Table of Contents
-----------------

[](#table-of-contents)

- [Quick Start](#quick-start)
- [MySQL Server Configuration](#mysql-server-configuration)
- [Installation](#installation)
- [Usage](#usage)
- [Event Subscribers](#event-subscribers)
- [Event Routes](#event-routes)
- [Management Commands](#management-commands)
- [Thanks to](#thanks-to)

MySQL Server Configuration
--------------------------

[](#mysql-server-configuration)

### Replication Settings

[](#replication-settings)

In your MySQL server configuration file, you need to enable replication:

```
[mysqld]
server-id        = 1
log_bin          = /var/log/mysql/mysql-bin.log
expire_logs_days = 10
max_binlog_size  = 100M
binlog_row_image = full
binlog-format    = row #Very important if you want to receive write, update and delete row events
```

For more information: [MySQL replication events explained](https://dev.mysql.com/doc/internals/en/event-meanings.html)

### User Privileges

[](#user-privileges)

Grant the necessary privileges to your MySQL user:

```
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'user'@'host';

GRANT SELECT ON `dbName`.* TO 'user'@'host';
```

Installation
------------

[](#installation)

### Laravel

[](#laravel)

Install via Composer:

```
composer require "huangdijia/laravel-trigger:^4.0"
```

Publish the configuration file:

```
php artisan vendor:publish --provider="Huangdijia\Trigger\TriggerServiceProvider"
```

### Lumen

[](#lumen)

Install via Composer:

```
composer require "huangdijia/laravel-trigger:^4.0"
```

Edit `bootstrap/app.php` and add:

```
$app->register(Huangdijia\Trigger\TriggerServiceProvider::class);
...
$app->configure('trigger');
```

Publish configuration and routes:

```
php artisan trigger:install [--force]
```

### Configure

[](#configure)

Edit your `.env` file and add the following configuration:

```
TRIGGER_HOST=192.168.xxx.xxx
TRIGGER_PORT=3306
TRIGGER_USER=username
TRIGGER_PASSWORD=password
...
```

### Connection Timeouts (Recommended)

[](#connection-timeouts-recommended)

If your MySQL server (or a proxy in front of it) disconnects idle clients (low `wait_timeout` / `interactive_timeout`), the trigger process may crash with errors like:

`SQLSTATE[HY000] ... 4031 The client was disconnected by the server because of inactivity.`

To improve stability, enable a keepalive ping and/or set session variables for the trigger's MySQL metadata connection:

```
# Ping MySQL periodically (seconds). Set to 0 to disable.
TRIGGER_KEEPALIVE=60

# Session variables applied on connect (comma-separated key=value pairs).
TRIGGER_SESSION_VARIABLES=wait_timeout=7200,interactive_timeout=7200,net_read_timeout=3600,net_write_timeout=3600
```

Usage
-----

[](#usage)

Start the trigger service to begin listening for MySQL events:

```
php artisan trigger:start [-R=xxx]
```

The service will monitor your MySQL binary log and trigger registered event handlers when database changes occur.

Event Subscribers
-----------------

[](#event-subscribers)

Create a custom event subscriber by extending the `EventSubscriber` class:

```
