How to do a simple Audit System in Laravel 5.5

Well, in this post I will show how I made a simple audit system using the jenssegers/agent package to get us the stats of system.

To begin, we will need to define what our system will track, in my case I needed log every access to system and track:

  • user
  • method / verb
  • path / uri
  • querystring
  • userAgent
  • ip
  • device
  • platform
  • browser
  • isDesktop
  • isMobile
  • isTablet
  • timestamps

Let’s start!!

Start new project to demonstrate it.

Please update the composer before continue 😉

Now we will use the jenssegers/agent package

When it’s done, you need to add it to container, edit your config/app.php

Add the service provider:

And add the Agent alias

Let’s see if everything is going well,

open your server

Is expected that you see Laravel default installation view.

Database and some configuration

In this example, we will use SQLite database. Please create it:

Now you need to made some changes in your .env file, change these variables.

Change your config/database.php in line 38 change the database variable to

When you make the migrations, you will check the connection.

Your output needs to be similar it:

As we will need the user in our audit system, please execute the following command:

Now your page have the authentication system with register and login routes.

Our system only track the authenticated users.

Please start the track!!!!!!!!!

No more blablabla or configuration, let’s do it!!!

Firstly we need a Model with migration.

Now, open your migration file …create_audit…. and add the coloumns:

After, you need to migrate it.

Results in

Make some changes in your Model, to get OnlineUsers and also define the relationship between user and log.

Update the App\User Model to add the relationship, insert this method.

Now we had the needed changes in our model and database migration, its time to create the Middleware to insert records in database.

It will create the middleware ( app\Http\Middleware\Audit.php), don’t forgot to register it in your Kernel (app\Http\Kernel.php) in web array of middlewareGroups.

Finally, we will update our middleware app\Http\Middleware\Audit.php

Now you can view online users and also the activities of each user. ONLY when a user has signed up and logged in our application.

To finish, add these Routes in your routes/web.php to test the system

It’s obvious that you can improve it, adding graphs and do something more with the recorded data.

Well the source are in github  https://github.com/diegosm/laravel-simple-audit-example

See you soon 😉

Share this!




4 Comments

You need to alter table to accept null on user_id:

$table->integer(‘user_id’)->unsigned()->nullable();

and remove the if from Middleware
if ( \Auth::check() ) {
}

I think is it 😉

Olá, existe alguma forma para que eu selecione somente uma rota específica para este Audit?
Por exemplo.

Tenho rotas:
/xxx
/xxx1
/xxx2
/xxx3

Quero que o audit só grave quando a rota /xxx3 for acessada.

Boaaa, o que pode ser feito é adicionar o middleware apenas ao grupo de rotas que deseja ou a rota em questão, mas para isto funcionar deve-se tirar o middleware do kernel e grupo web (pois aplicaria-se a todas as rotas web). Desculpa o atraso na resposta, estava de mudança

Leave a Reply

Your email address will not be published. Required fields are marked *

*