shiftonelabs / laravel-db-events
Add extra database events.
Installs: 8 353
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 5
Open Issues: 0
pkg:composer/shiftonelabs/laravel-db-events
Requires
- php: >=5.4.0
- illuminate/database: >=4.1
- illuminate/support: >=4.1
Requires (Dev)
- laravel/framework: >=4.1
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.0
- squizlabs/php_codesniffer: 2.*
This package is auto-updated.
Last update: 2025-10-13 10:06:16 UTC
README
This Laravel/Lumen package provides additional events for the Illuminate Database package. Currently, this package adds:
- a
DatabaseConnectingevent that is fired before connecting to the database, which can modify the configuration or cancel the connection - a
DatabaseConnectedevent that is fired after connecting to the database - a
ConnectingExceptionruntime exception that is thrown if the database connection is cancelled by theDatabaseConnectingevent
Additional events may be added be added as requested/submitted.
Install
Via Composer
$ composer require shiftonelabs/laravel-db-events
Once composer has been updated and the package has been installed, the service provider will need to be loaded.
For Laravel 4, open app/config/app.php and add following line to the providers array:
'ShiftOneLabs\LaravelDbEvents\LaravelDbEventsServiceProvider',
For Laravel 5, open config/app.php and add following line to the providers array:
ShiftOneLabs\LaravelDbEvents\LaravelDbEventsServiceProvider::class,
For Lumen 5, open bootstrap/app.php and add following line under the "Register Service Providers" section:
$app->register(ShiftOneLabs\LaravelDbEvents\LaravelDbEventsServiceProvider::class);
Usage
DatabaseConnecting Event
The ShiftOneLabs\LaravelDbEvents\Extension\Database\Events\DatabaseConnecting event allows you to hook into the database connection lifecycle before the connection is established. Additionally, this event provides you the ability to modify the configuration used for the connection, as well as completely cancel the connection attempt.
Attributes
The DatabaseConnecting event provides three public attributes:
| Attribute | Description |
|---|---|
public $connector |
The Connector object making the connection. This package extends each of the built-in connectors. |
public $connectionName |
The name of the selected database connection configuration. |
public $config |
The configuration array used to connect to the database. |
Example:
app('events')->listen('ShiftOneLabs\LaravelDbEvents\Extension\Database\Events\DatabaseConnecting', function ($event) { app('log')->info('Connector class: '.get_class($event->connector)); app('log')->info('Connection name: '.$event->connectionName); app('log')->info('Configuration: '.print_r($event->config, true)); });
Modifying Connection Configuration
The configuration for your database connections is usually stored in your config/database.php file (in conjunction with your .env file). If, however, you need to dynamically modify the configuration used for the connection, this can be done inside a DatabaseConnecting event listener. Any changes made to the configuration in the event listener will be used for the database connection.
Example:
app('events')->listen('ShiftOneLabs\LaravelDbEvents\Extension\Database\Events\DatabaseConnecting', function ($event) { // don't connect to mysql in strict mode if you like zeroed out dates if (i_like_zero_dates()) { $event->config['strict'] = false; } });
Cancelling the Connection
There may be situations where you would like to prevent the database from attempting the connection. In this case, the database connection attempt can be cancelled by returning false from a DatabaseConnecting event listener. If the database connection is cancelled, a ShiftOneLabs\LaravelDbEvents\Exceptions\ConnectingException runtime exception will be thrown.
Example:
app('events')->listen('ShiftOneLabs\LaravelDbEvents\Extension\Database\Events\DatabaseConnecting', function ($event) { if (not_todaaay()) { return false; } });
DatabaseConnected Event
The ShiftOneLabs\LaravelDbEvents\Extension\Database\Events\DatabaseConnected event allows you to hook into the database connection lifecycle after the connection is established. Additionally, this event provides access to the final configuration used for the connection, as well as the connection itself.
Attributes
The DatabaseConnected event provides four public attributes:
| Attribute | Description |
|---|---|
public $connector |
The Connector object making the connection. This package extends each of the built-in connectors. |
public $connectionName |
The name of the selected database connection configuration. |
public $config |
The configuration array that was used to connect to the database. |
public $pdo |
The connected PDO (or potentially Doctrine\DBAL\Driver\PDOConnection, as of 5.3) object. |
Example:
app('events')->listen('ShiftOneLabs\LaravelDbEvents\Extension\Database\Events\DatabaseConnected', function ($event) { app('log')->info('Connector class: '.get_class($event->connector)); app('log')->info('Connection name: '.$event->connectionName); app('log')->info('Configuration: '.print_r($event->config, true)); app('log')->info('PDO class: '.get_class($event->pdo)); });
Contributing
Contributions are very welcome. Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email patrick@shiftonelabs.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.