Wallogit.com
2017 © Pedro Peláez
Watchable is a Laravel package where you can easily pick up laravel model event and activity log inside your application., (*1)
use Shipu\Watchable\Traits\WatchableTrait;
onModelCreating onModelCreated onModelUpdating onModelUpdated
The Package stores all activity in the activity_logs table.
Here's a demo of how you can use it:, (*2)
activity()->log('Look, I logged something');
You can retrieve all activity using the Shipu\Watchable\Models\Activity model., (*3)
Activity::all();
Here's a more advanced example:, (*4)
activity()
->on($anEloquentModel)
->data($storeWhatYouWantTo)
->log('Look, I logged something');
$lastLoggedActivity = Activity::all()->last();
$lastLoggedActivity->model; //returns an instance of an eloquent model
$lastLoggedActivity->causer; //returns an instance of your user model
$lastLoggedActivity->remarks; //returns 'Look, I logged something'
Here's an example on event logging., (*5)
$user->name = 'updated name'; $user->save(); //updating the newsItem will cause the logging of an activity $activity = Activity::all()->last(); $activity->remarks; //returns 'User Updated' $activity->model; //returns the instance of NewsItem that was created
Calling $activity->changes will return this array:, (*6)
[
'new' => [
'name' => 'updated name',
'text' => 'Lorum',
],
'old' => [
'name' => 'original name',
'text' => 'Lorum',
],
];
You can install the package via composer: ``` bash composer require shipu/watchable, (*7)
You can optionally publish the config file with: ```bash php artisan vendor:publish --provider="Shipu\Watchable\WatchableServiceProvider" --tag="shipu-watchable-config"
This is the contents of the published config file:, (*8)
return [
'audit_columns' => [
'creator_column' => 'creator',
'editor_column' => 'editor',
'default_active' => false,
],
'activity_log' => [
'model' => \Shipu\Watchable\Models\Activity::class
]
];
You can publish the migration with:, (*9)
php artisan vendor:publish --provider="Shipu\Watchable\WatchableServiceProvider" --tag="shipu-watchable-migrations"
Note: The default migration assumes you are using integers for your model IDs. If you are using UUIDs, or some other format, adjust the format of the subject_id and causer_id fields in the published migration before continuing., (*10)
After publishing the migration you can create the activity_logs table by running the migrations:, (*11)
php artisan migrate