PHP code example of codemyviews / activitylog

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

    

codemyviews / activitylog example snippets


// config/app.php

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

// config/app.php

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

// at the top of your file, you should import the facade.
use Activity;
...
/*
  The log function takes three parameters:
      - $text: the activity you wish to log.
      - $user:  optional can be a user id or a user object.
                if not proved the id of Auth::user() will be used
      - $model: optional can be an instance of Illuminate\Database\Eloquent\Model
                if not provided, none will be used
                if provided, activity will be referencing this model
*/
Activity::log('Some activity that you wish to log');
Activity::log('User has updated a post', $user, $post);

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 CodeMyViews\Activitylog\Handlers\BeforeHandlerInterface;

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

        return true;
    }
}

use CodeMyViews\Activitylog\Models\Activity;

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

Activity::cleanLog();

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

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