PHP code example of spatie / activitylog

1. Go to this page and download the library: Download spatie/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/ */

    

spatie / activitylog example snippets


// config/app.php

'providers' => [
    '...',
    'Spatie\Activitylog\ActivitylogServiceProvider',
];

// config/app.php

'aliases' => [
	...
	'Activity' => 'Spatie\Activitylog\ActivitylogFacade',
];

//at the top of your file you should import the facade.
use Activity;
...
/*
  The log-function takes two parameters:
  	- $text: the activity you wish to log.
  	- $user: optional can be an user id or a user object.
  	         if not proved the id of Auth::user() will be used

*/
Activity::log('Some activity that you wish to log');

use Spatie\Activitylog\LogsActivityInterface;
use Spatie\Activitylog\LogsActivity;

class Article implements LogsActivityInterface {

   use LogsActivity;
...

/**
 * Get the message that needs to be logged for the given event name.
 *
 * @param string $eventName
 * @return string
 */
public function getActivityDescriptionForEvent($eventName)
{
    if ($eventName == 'created')
    {
        return 'Article "' . $this->name . '" was created';
    }

    if ($eventName == 'updated')
    {
        return 'Article "' . $this->name . '" was updated';
    }

    if ($eventName == 'deleted')
    {
        return 'Article "' . $this->name . '" was deleted';
    }

    return '';
}

'beforeHandler' => '\App\Handlers\BeforeHandler',



namespace App\Handlers;

use Spatie\Activitylog\Handlers\BeforeHandlerInterface;

class BeforeHandler implements BeforeHandlerInterface
{
    public function shouldLog($text, $userId)
	{
		if ($userId == 1) return false;

		return true;
	}
}

use Spatie\Activitylog\Models\Activity;

$latestActivities = Activity::with('user')->latest()->limit(100)->get();

Activity::cleanLog();

php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="migrations"
php artisan migrate

php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="config"
config/activitylog.php