Simple Data Model implementation package for Laravel using MySQL.
To start using this package, install it with composer:
composer require CronxCo/data-modelPublishing config and migrations:
php artisan vendor:publish --provider=CronxCo\DataModel\DataModelServiceProvider
This package uses Laravel's package auto-discovery feature, so there is no need to modify your config/app.php file.
data model logs are saved to your main database by default, but it is recommended to use a dedicated MySQL database for it. Once you create the database, make sure to set data model to use it:
First, add a dedicated connection to your config/database.php file:
'connections' => [
/*
...
*/
'DataModel' => [
'driver' => 'mysql',
'host' => env('DATA_MODEL_HOST', 'localhost'),
'port' => env('DATA_MODEL_PORT', '3306'),
'database' => env('DATA_MODEL_DATABASE', 'data_model'),
'username' => env('DATA_MODEL_USERNAME', 'root'),
'password' => env('DATA_MODEL_PASSWORD', 'root'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
/*
...
*/
]Next, add required environment variables to your .env file:
DATA_MODEL_CONNECTION="DataModel"
DATA_MODEL_DATABASE="your_data_model_database_name"
DATA_MODEL_TABLE="your_data_model_table_name"
DATA_MODEL_USERNAME="your_data_model_user_name"
DATA_MODEL_PASSWORD="your_data_model_user_password"It is recommended to create a separate user for the data model database, and remove UPDATE, DELETE, DROP permissions, to make sure your data model is append-only.
Next, run the migration to create default data model table:
php artisan migrate
To start logging your events, append this line to your code where you wish the event to be logged:
DataModel::add('event_name', $data);Or using the DataModel helper function, which is just a wrapper for the facade:
DataModel()->add('event_name', $data);the add() method accepts four arguments:
$event_action: name of your event, e.g.user_created,email_sent,order_shipped, etc.$event_value: array of values to record. e.g. foruser_createdevent, you can pass the array of attributes that this user was created with.$target_id: (optional) ID of target model in your database. E.g., foremail_sentevent, you can passuser_idas$target_id. This helps in the future when you wish to fetch all events related to a particular user.$before: (optional) array of values that were changed. E.g. foruser_updatedevent, you may pass$user->toArray()to record attributes that were changed and their values before the change. Note: theadd()method automatically filters out only those keys that exist in$event_valueparameter to avoid unnecessary overhead.
Returns Illuminate\Database\Eloquent\Builder instance so you can perform any query on data model tables.
Gets all events from the default data model table. Returns a collection.
Gets all events of specific type from data model table. Automatically determines which table to search in. Returns a collection.
Sets dedicated table and returns Illuminate\Database\Eloquent\Builder instance so you can perform any query on data model tables.
By default, DataModel suppresses any exceptions that occur during add() method call. You can disable this by changing throw_exceptions setting in config/DataModel.php:
'throw_exceptions' => true,Run the tests with
vendor/bin/phpunit
