PHP code example of learncodeweb / activitylog

1. Go to this page and download the library: Download learncodeweb/activitylog library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

learncodeweb / activitylog example snippets


/*
 * Package Service Providers...
 */
Lcw\Activitylog\Providers\ActivityLogProvider::class,



return [
    App\Providers\AppServiceProvider::class,
    Lcw\Activitylog\Providers\ActivityLogProvider::class, // ✅ Add this line
];



return [

    /*
    |--------------------------------------------------------------------------
    | Delete Limit
    |--------------------------------------------------------------------------
    | Set how many months of old logs to keep before auto-deletion.
    | Default: 3 months
    */
    'delete_limit' => 3,

    /*
    |--------------------------------------------------------------------------
    | Ignore Routes
    |--------------------------------------------------------------------------
    | List route names or URIs where you do NOT want activity to be logged.
    | Example: dashboard index, file downloads, etc.
    */
    'ignore_routes' => [
        'dashboard.index',
        'settings.download.history.activity_log',
    ],

];

use Lcw\Activitylog\ActivityLog;

public function index(Request $request)
{
    $activityLog = new ActivityLog();
    $logs = $activityLog->get($request);

    return view('your-view', compact('logs'));
}

use Lcw\Activitylog\ActivityLog;

$activityLog = new ActivityLog();

// Delete logs older than 1 month
$activityLog->logDelete(1);

// Delete logs older than 3 months
$activityLog->logDelete(3);

use Lcw\Activitylog\ActivityLog;

$activityLog = new ActivityLog();

$activityLog->create([
    'log'          => 'User updated their notification settings',
    'query_string' => $request->all(),
]);

'ignore_routes' => [
    'dashboard.index',           // by route name
    'api/health-check',          // by URI
    'settings.download.history.activity_log',
],



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Lcw\Activitylog\ActivityLog;

class ActivityLogController extends Controller
{
    protected $activityLog;

    public function __construct()
    {
        $this->activityLog = new ActivityLog();
    }

    // Show all activity logs
    public function index(Request $request)
    {
        $logs = $this->activityLog->get($request);
        return view('admin.activity-log', compact('logs'));
    }

    // Delete logs older than X months
    public function deleteOld(Request $request)
    {
        $months = $request->input('months', 3);
        $this->activityLog->logDelete($months);

        return back()->with('success', 'Old logs deleted successfully!');
    }

    // Manually log a custom action
    public function logCustomAction()
    {
        $this->activityLog->create([
            'log' => 'Admin exported the user report',
        ]);

        return back()->with('success', 'Action logged!');
    }
}

'migration_parameters' => [
    '--path'     => ['/database/migrations/tenant'],
    '--realpath' => true,
    '--force'    => true,
],

use Stancl\Tenancy\Events;
use Stancl\Tenancy\Jobs\MigrateDatabase;

'listeners' => [
    Events\TenantCreated::class => [
        MigrateDatabase::class, // ✅ Naye tenant par auto migrate hoga
    ],
],

use Lcw\Activitylog\ActivityLog;

$activityLog = new ActivityLog();

// Logs fetch karo (current tenant ka database use hoga automatically)
$logs = $activityLog->get($request);

// Custom log banana
$activityLog->create([
    'log' => 'User ne invoice download ki',
]);

// Purane logs delete karo
$activityLog->logDelete(3);

use Lcw\Activitylog\ActivityLog;

$tenant = \App\Models\Tenant::find('tenant-id');

tenancy()->initialize($tenant);

$activityLog = new ActivityLog();
$logs = $activityLog->get($request);

tenancy()->end();

return view('admin.tenant-logs', compact('logs'));
bash
php artisan vendor:publish --provider="Lcw\Activitylog\Providers\ActivityLogProvider" --tag="migrations"
bash
php artisan migrate
bash
php artisan vendor:publish --provider="Lcw\Activitylog\Providers\ActivityLogProvider" --tag="config"

src/
├── ActivityLog.php              # Core activity log class
├── Providers/
│   └── ActivityLogProvider.php  # Service provider
├── Http/
│   └── Middleware/              # Auto-logging middleware
├── Models/
│   └── ActivityLogModel.php     # Eloquent model
├── database/
│   └── migrations/              # Migration files
├── config/
│   └── lcw_activity_log_config.php  # Config file
└── resources/
    └── views/                   # Default log viewer blade templates
bash
# Pehle publish karo
php artisan vendor:publish --provider="Lcw\Activitylog\Providers\ActivityLogProvider" --tag="migrations"

# Phir tenant folder mein move karo
mv database/migrations/*_create_activity_log_master.php database/migrations/tenant/
bash
# Sab tenants par migrate karo
php artisan tenants:migrate

# Sirf ek specific tenant par migrate karo
php artisan tenants:migrate --tenants=your-tenant-id