This is as simple as keeping a diary for your models!
You can record a short message for any model with current timestamp and authenticated user.
A) Execute composer require igaster/laravel-model-events
This package includes a Service Provider that will be automatically discovered by Laravel.
B) Run migrations.
This will create a table log_model_events
that will be used to store events.
use Igaster\ModelEvents\Traits\LogsModelEvents;
class MyModel extends Model
{
use LogsModelEvents;
Use the logModelEvent("Description")
method to log any event
class MyModel extends Model
{
public function myMethod()
{
// ...
$modelEvent = $this->logModelEvent("Something Happened!");
}
- The
logModelEvent()
method will also log a) the current authenticated user and b) the related model instance c) current timestamp - This is a public method. You may also call it from your
$model
instance from anywhere
Eloquent models fire several events during updating, creating etc. These events can be automatically logged. Just define these events inside the $logModelEvents
static array in your model:
class MyModel extends Model
{
public static $logModelEvents = [
'created',
'updated',
];
- Now every time this model instance is changed, the event will be logged and attributed to the authenticated user.
- As a bonus a report of all the updated attributes will be added in the description!
// This will retrieve the last 10 events logged for $model instance.
$modelEvents = $model->getModelEvents(10);
In order to query events from a $user model you must first include this trait with the User class: Note: This trait is optional for the rest functions of this package!
use Igaster\ModelEvents\Traits\UserLogsModelEvents;
class User extends Authenticatable
{
use UserLogsModelEvents;
// This will retrieve the last 10 events logged by this $user.
$modelEvents = $user->getUserModelEvents(10);
All relationships with the LogModelEvent
model have been implemented. These are some valid queries:
$user->modelEvents; // Get all model events for $user
$model->modelEvents; // Get all model events for $model
$model->modelEvents()->where(`created_at`, '>', $yesterday)->get(); // Custom Query
// Or you can build queries with the LogModelEvent model:
LogModelEvent::whereUser($user)->whereModel($model)->get();
Through a LogModelEvents
model you can retrieve the $user
and the $model
instances:
foreach($model->modelEvents as $modelEvent){
$modelEvent->user; // User model
$modelEvent->model; // Model related with the event (though polymorphic relathinships)
$modelEvent->description; // String
$modelEvent->created_at; // Timestamp
}
Note the the $modelEvent->model
is a polymorphic relationship and it will retrieve a $model
instance on its respective class.
You may include the model-events::modelEvents
partial in your views to render a list of events:
<div class="row">
<div class="col-md-12">
<h4>Actions History:</h4>
@include('model-events::modelEvents', [
'model' => $order
])
</div>
</div>
Available parameters are: model
, user
, count_events
. All are optional