Laravel Events is a package designed to simplify event handling in Laravel applications. It provides tools for logging, rate-limiting, and syncing events between files and the database.
- Log events to files or the database.
- Rate-limit event handling to prevent abuse.
- Sync events between files and the database.
- Support for custom event types and levels.
- Automatically generates unique and sequential identifiers for events.
- Includes database migrations for event storage.
You can install the package via composer:
composer require juniorfontenele/laravel-eventsAfter installation, run the migrations to set up the necessary database tables:
php artisan migrateIf you want to customize the migrations, you can publish them using the following command:
php artisan vendor:publish --tag=laravel-events-migrationsThe migrations will be published to the database/migrations directory, where you can modify them as needed.
Publish the configuration file to customize the package behavior:
php artisan vendor:publish --tag=laravel-events-configThe configuration file will be published to config/events.php. You can customize settings such as rate-limiting and default event behavior.
Create events using the Artisan command provided by the package:
php artisan make:event EventNameThis command will generate a new event file in the appropriate directory with a unique and sequential identifier for traceability. The generated file will include all necessary properties, such as id, name, type, level, and description.
Example of a generated event file:
use JuniorFontenele\LaravelEvents\BaseEvent;
use JuniorFontenele\LaravelEvents\Enums\EventType;
use JuniorFontenele\LaravelEvents\Enums\EventLevel;
class UserRegistered extends BaseEvent
{
public int $id = 1; // Automatically generated
public string $name = 'User Registered';
public EventType $type = EventType::USER;
public EventLevel $level = EventLevel::INFO;
public string $description = 'Triggered when a user registers.';
public bool $shouldLog = true;
public bool $shouldWriteToDatabase = true;
}Use the WriteEventsToLog listener to log events to the Laravel log system. Ensure your event implements the shouldLog method.
Use the WriteEventsToDatabase listener to persist events to the database. Ensure your event implements the shouldWriteToDatabase method.
The HasRateLimiter trait provides methods to limit the number of times an event can be handled within a specific time frame. Configure rate-limiting in config/events.php.
The package provides several Artisan commands:
php artisan events:sync-from-fileThis command updates the database with the information from the event files located in the App\Events directory. It ensures that all events defined in files are properly registered in the database. This is useful when new events are added or existing ones are modified in the codebase.
php artisan events:sync-from-dbThis command generates event files for events that exist in the database but are missing in the App\Events directory. It ensures that all events in the database have corresponding files in the codebase. This is useful for restoring missing event files.
php artisan events:clear-oldThis command deletes events older than a specified number of days (default is 180 days). The maximum age can be configured in config/events.php under the max_days setting. This command can be added to the Laravel scheduler to automatically clean up old event records:
$schedule->command('events:clear-old')->daily();php artisan events:reorder-idsThis command reorders the event IDs in the database to ensure they are sequential. This is useful when events have been deleted, and you want to maintain a clean, sequential order of IDs. Use the --force option to skip confirmation:
php artisan events:reorder-ids --forcephp artisan events:listList all events stored in the database.
Run the tests with:
composer testPlease see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.