Laravel 9 User Activity Log Example Tutorial

Laravel 9 user Activity log example; Through this tutorial, i am going to show you how to create user activity log in laravel 9 apps using haruncpi/laravel-user-activity log package.

Laravel 9 User Activity Log Example Tutorial

Follow the below given steps to create user activity log in laravel 9 apps:

  • Step 1 – Install Laravel 9 App
  • Step 2 – Configure Database with App
  • Step 3 – Install Laravel UI
  • Step 4 – Install Bootstrap Auth Scaffolding
  • Step 5 – Install Npm Packages
  • Step 6 – Install And Configure User Activity Log Package
  • Step 7 – Run PHP artisan Migrate
  • Step 8 – Run Development Server
  • Step 9 – Test This App

Step 1 – Install Laravel 9 App

Run the following command on command prompt to navigate to your local web server directory:

//for windows user
cd xampp/htdocs

//for ubuntu user
cd var/www/html

Run the following command on command prompt to install laravel latest application:

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

Step 2 – Configure Database with App

Go to downloaded laravel app into any text editor. Then find .env file and configure database detail like following:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db name
DB_USERNAME=db user name
DB_PASSWORD=db password

Step 3 – Install Laravel UI

Run the following command on command prompt to install LARAVEL ui package in laravel app:

composer require laravel/ui

Step 4 – Install Bootstrap Auth Scaffolding

Run the following command on command prompt to install auth scaffolding bootstrap package in laravel app:

php artisan ui bootstrap --auth

Step 5 – Install Npm Packages

Run the following command on command prompt to install node js:

npm install

Then type the following command on cmd to run npm:

npm run dev

Step 6 – Install And Configure User Activity Log Package

Run the following command on command prompt to install user activity log package:

composer require haruncpi/laravel-user-activity
php artisan user-activity:install

After that, visit app/models directory and open user.php model and add the following line of code into it:

<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;
use Haruncpi\LaravelUserActivity\Traits\Loggable;
class User extends Authenticatable
{
    use HasApiTokens;
    use HasFactory;
    use HasProfilePhoto;
    use Notifiable;
    use TwoFactorAuthenticatable;
     use Loggable;
    /**
     * 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',
        'two_factor_recovery_codes',
        'two_factor_secret',
    ];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = [
        'profile_photo_url',
    ];
}

Step 7 – Run php artisan Migrate

Run following command on command prompt on it to create database table:

php artisan migrate

Step 8 – Run Development Server

Run the following command on command prompt to start development server:

php artisan serve

Step 9 – Test This App

Now, open browser and hit the following url on it:

http://127.0.0.1:8000/

If you want to check user activity log, you can hit the following url on browser:

http://example.com/admin/user-activity

Note that, you can also delete user activity log by executing the following command on terminal:

//for 7 days
php artisan user-activity:delete
//for 30 days
php artisan user-activity:delete 30
//for all days
php artisan user-activity:delete all

Recommended Laravel Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.