Custom User Log Activity in Laravel App Example

Custom User Log Activity in Laravel App Example

In this post we will give you information about Custom User Log Activity in Laravel App Example. Hear we will give you detail about Custom User Log Activity in Laravel App ExampleAnd how to use it also give you demo for it if it is necessary.

In this tutorial we will learn how to create custom user log activity without composer page in laravel application.

In this article, I share with you very simple and easy log management in laravel application. I created custom log tables and helper facade that way you can keep your track user logs. You can simply modify table structure and also helper method name etc as you like. I always like to do something new so i did very simple user custom log activity management system.

After reviewing all codes, you can understand how it works and simple. we will manage log without using anymore package. In this example i created “log_activities” table with following column as listed bellow:

1)ID

2)Subject

3)URL

4)Method

5)IP

6)Agent

7)user_id

8)created_at

9)updated_at

As listed above fields. I always put on log, so you can simply modify table structure and add new fields. I create LogActivity helper for put on log helper. So just follow few step and get very basic example of keep log activity.

After creating successfully this example, you will have layout as like bellow screen shot.

Preview:

Step 1 : Install Laravel Fresh Application

we are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Database Configuration

In this step we have to make database configuration for example database name, username, password etc. So let’s open .env file and fill all details like as bellow:

.env

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

Also see:Laravel – generate PDF from html view file and download using dompdf

Step 3: Create LogActivity Table and Model

In this step we have to create migration for log_activities table using Laravel 5.4 php artisan command, so first fire bellow command:

php artisan make:migration create_log_activity_table

After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create contactus table.

<?php


use IlluminateSupportFacadesSchema;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;


class CreateLogActivityTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('log_activities', function (Blueprint $table) {

$table->increments('id');

$table->string('subject');

$table->string('url');

$table->string('method');

$table->string('ip');

$table->string('agent')->nullable();

$table->integer('user_id')->nullable();

$table->timestamps();

});

}


/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::drop('log_activities');

}

}

Now run above migration by following command:

php artisan migrate

After creating table we have to create model for “log_activities” table so just run bellow command and create new model:

php artisan make:model LogActivity

Ok, so after run bellow command you will find app/LogActivity.php and put bellow content in LogActivity.php file:

app/LogActivity.php

<?php


namespace App;


use IlluminateDatabaseEloquentModel;


class LogActivity extends Model

{

/**

* The attributes that are mass assignable.

*

* @var array

*/

protected $fillable = [

'subject', 'url', 'method', 'ip', 'agent', 'user_id'

];

}

Step 4: Create LogActivity Helper Class


In this step we will create new directory “Helpers” in App directory. After created Helpers folder we require to create create LogActivity.php file and put bellow code:

app/Helpers/LogActivity.php

<?php


namespace AppHelpers;

use Request;

use AppLogActivity as LogActivityModel;


class LogActivity

{


public static function addToLog($subject)

{

$log = [];

$log['subject'] = $subject;

$log['url'] = Request::fullUrl();

$log['method'] = Request::method();

$log['ip'] = Request::ip();

$log['agent'] = Request::header('user-agent');

$log['user_id'] = auth()->check() ? auth()->user()->id : 1;

LogActivityModel::create($log);

}


public static function logActivityLists()

{

return LogActivityModel::latest()->get();

}


}

Step 5: Register Helper Class

In this step we have to register Helper class as facade in configuration file. So let’s open app.php file and add Helper facade with load class. open app.php file and add bellow Helper facade line.

config/app.php

....

'aliases' => [

....

'LogActivity' => AppHelpersLogActivity::class,

]

Step 6: Add Route

In this step we will add two new routes. i added “add-to-log” route for testing you can simply use addToLog() with subject title like you can add “User created Successfully”, “User updated successfully” etc subject as argument. another route for listing on user activity logs what was did by user. So let’s add bellow two route.

routes/web.php

Route::get('add-to-log', 'HomeController@myTestAddToLog');

Route::get('logActivity', 'HomeController@logActivity');

Step 7: Add Controller Method

In this step, we will add new two methods in HomeController file. i write how to add log on logs table and listing. So let’s add bellow code.

app/Http/Controllers/HomeController.php

<?php


namespace AppHttpControllers;


use IlluminateHttpRequest;


class HomeController extends Controller

{

/**

* Create a new controller instance.

*

* @return void

*/

public function __construct()

{


}

/**

* Show the application dashboard.

*

* @return IlluminateHttpResponse

*/

public function myTestAddToLog()

{

LogActivity::addToLog('My Testing Add To Log.');

dd('log insert successfully.');

}


/**

* Show the application dashboard.

*

* @return IlluminateHttpResponse

*/

public function logActivity()

{

$logs = LogActivity::logActivityLists();

return view('logActivity',compact('logs'));

}

}

Step 8: Add View File

In last step, we will create logActivity.blade.php file for display all logs with details form table. So let’s copy from bellow code and put.

resources/views/logActivity.php

<!DOCTYPE html>

<html>

<head>

<title>Log Activity Lists</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />

</head>

<body>


<div >

<h1>Log Activity Lists</h1>

<table >

<tr>

<th>No</th>

<th>Subject</th>

<th>URL</th>

<th>Method</th>

<th>Ip</th>

<th width="300px">User Agent</th>

<th>User Id</th>

<th>Action</th>

</tr>

@if($logs->count())

@foreach($logs as $key => $log)

<tr>

<td>{{ ++$key }}</td>

<td>{{ $log->subject }}</td>

<td >{{ $log->url }}</td>

<td><label >{{ $log->method }}</label></td>

<td >{{ $log->ip }}</td>

<td >{{ $log->agent }}</td>

<td>{{ $log->user_id }}</td>

<td><button >Delete</button></td>

</tr>

@endforeach

@endif

</table>

</div>


</body>

</html>

Now we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

Also see:How to create ban/revoke user functionality in Laravel 5 example ?

http://localhost:8000/add-to-log

http://localhost:8000/logActivity

I hope it can help you…

Hope this code and post will helped you for implement Custom User Log Activity in Laravel App Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

  +  17  =  20

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US