Spatie's Activity Logger: A Must-Have for Laravel

How to Log Activity Using Laravel Spatie

Activity Logger - Laravel Spatie

In this aricle we will learn how to log users activtity in database using Laravel Spatie Package . You can log every activity of a user like when a user is created,updated or deleted . So Spatie makes this easier for us to track an user's activity easily . 

We will learn about this package with example , so just follow the steps and learn how to use Spatie package to log user's activity .

Table of Content :

  • Install Spatie Package
  • Make a Demo Log
  • Log with Models

Step 1 - Install Laravel Spatie :

The very first step is to install Laravel Spatie Package . Use the Following commands to install Laravel Spatie Package .

composer require spatie/laravel-activitylog

Publish your migration file using the following command .

php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="migrations"

Now run migrate command to create a activity_log table in the database .

php artisan migrate

Note : 

If you are getting error during migration then use the command php artisan config:cache and restart your server .

Step 2 - Make a Demo Log :

Let's check whether the package is working or not , Let's make a demo log using php artisan tinker  . Just use the following commands on your command prompt to make a demo log .

php artisan tinker

Use the following command to make a demo activity .

activity()->log('Look mum, I logged something');

Activity Logger - Laravel Spatie

Now you can check your activity_log table on your database , you will get a log detail like the following .
Activity Logger - Laravel Spatie

Step 3 - Log with Model :

You can use this package on any of your model , so that you will be able to track every activity of that model . Here we will implement activity log inside our User model .

Import LogsActivity class :

 First of all use the following class inside your User Model .

use Spatie\Activitylog\Traits\LogsActivity;

Use the following inside your User class .

use LogsActivity

User.php :

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Activitylog\Traits\LogsActivity;

class User extends Authenticatable
{
    use Notifiable,LogsActivity;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
}

Now it's done , now if you do anything inside your User model ( like create , delete , update ) it will create a activity log inside your activity_log table .

Let's create a user using php tinker and check whether it's logging the data or not  .

Use the following command to create a demo user using php artisan tinker .

php artisan tinker

factory(App\User::class,1)->create()

Activity Logger - Laravel Spatie


Now you can check your database it would have created a log for the new user creation .

Activity Logger - Laravel Spatie







Previous Post Next Post

Contact Form