PHP code example of rejtg21 / activitylog

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

    

rejtg21 / 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($data)
	{
		if ($data->user_id == 1) return false;

		return true;
	}
}

'afterHandler' => 'App\Http\Controllers\ActivityController',

 namespace App\Http\Controllers

use Spatie\Activitylog\Handlers\AfterHandlerInterface;

class ActivityController extends Controller implements AfterHandlerInterface
{

    public function shouldLogAfter($data)
    {
      // logic here
      return $data;
    }
}


  $data = [
    'partner_id' => 1
  ];

  Activity::log('successfully logged', $data);

public function shouldLogAfter($data)
{
  $data->partner_id;
  // other logic here
  return $data;
}

  public function shouldLogAfter($data)
  {
    $data->activity->id;
    $data->activity->ip_address;
    // model explanation below
    $data->activity->partner()->attach($data->partner_id);
  }

  $activity = Activity::log('successfully logged');

  /**
  * you can already access the object
  */
  $activty->text;
  $activity->id;

  // declare first the variable activityData
  public $activtyData = [];
  ...

  public function getActivityDescriptionForEvent($eventName)
    {
      // insert some data
        $this->activityData = [
            'partner_id' => $this->id,
        ];

        if($eventName == 'created')
            return 'successfully created';
    }


  'modelPath' => 'App\Models\Activity'

   namespace App\Models

  use Spatie\Activitylog\Models\Activity as Spatie;
  class Activity extends Spatie
  {
      public function partner()
      {
          return $this->belongsToMany('App\Models\User', 'user_activities',
                'activity_id', 'user_id');
      }

     //add other functions here ....
  }

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