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:










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.


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:





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.


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) {












* Reverse the migrations.


* @return void


public function down()





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:



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:



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;



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.



'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.


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.



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.


<!DOCTYPE html>



<title>Log Activity Lists</title>

<link rel="stylesheet" href="" />



<div >

<h1>Log Activity Lists</h1>

<table >







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

<th>User Id</th>




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


<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>








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 ?



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 *

7  +  3  =  

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