spatie/laravel-activitylog
Log user and model activity in Laravel with a simple API. Automatically record Eloquent events, track subjects and causers, attach custom properties, and query everything via the Activity model. Stores logs in the activity_log table.
Customization of how your models will be logged is controlled by implementing getActivitylogOptions(). This method is optional. If not implemented, the package uses sensible defaults (logs events but no attribute changes).
The most basic example of an Activity logged model is:
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Models\Concerns\LogsActivity;
class YourModel extends Model
{
use LogsActivity;
}
To customize what gets logged, override getActivitylogOptions():
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Models\Concerns\LogsActivity;
use Spatie\Activitylog\Support\LogOptions;
class YourModel extends Model
{
use LogsActivity;
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logFillable()
->logOnlyDirty();
}
}
The call to LogOptions::defaults() yields the following default options:
public ?string $logName = null;
public bool $logEmptyChanges = true;
public bool $logFillable = false;
public bool $logOnlyDirty = false;
public bool $logUnguarded = false;
public array $logAttributes = [];
public array $logExceptAttributes = [];
public array $dontLogIfAttributesChangedOnly = [];
public array $attributeRawValues = [];
public ?Closure $descriptionForEvent = null;
/**
* Start configuring model with the default options.
*/
public static function defaults(): LogOptions;
This method is equivalent to ->logOnly(['*']).
/**
* Log all attributes on the model.
*/
public function logAll(): LogOptions;
/**
* Log changes to all attributes that are not listed in $guarded.
*/
public function logUnguarded(): LogOptions;
This can be combined with ->logFillable() or ->logOnly(). The final set of logged attributes is the union of all sources.
/**
* Log changes to all the $fillable attributes of the model.
*/
public function logFillable(): LogOptions;
This can be combined with ->logUnguarded() or ->logOnly(). The final set of logged attributes is the union of all sources.
/**
* Stop logging $fillable attributes of the model.
*/
public function dontLogFillable(): LogOptions;
/**
* Log changes that have actually changed after the update.
*/
public function logOnlyDirty(): LogOptions;
/**
* Only log changes to these specific attributes.
*/
public function logOnly(array $attributes): LogOptions;
Convenient method for excluding specific attributes from logging.
/**
* Exclude these attributes from being logged.
*/
public function logExcept(array $attributes): LogOptions;
/**
* Don't trigger an activity if only these attributes changed.
*/
public function dontLogIfAttributesChangedOnly(array $attributes): LogOptions;
/**
* Don't store empty logs. Empty logs can occur when you're tracking
* specific attributes but none of them actually changed.
*/
public function dontLogEmptyChanges(): LogOptions;
/**
* Allow storing empty logs.
*/
public function logEmptyChanges(): LogOptions;
/**
* Customize log name.
*/
public function useLogName(string $logName): LogOptions;
/**
* Skip using mutators for these attributes when logged.
*/
public function useAttributeRawValues(array $attributes): LogOptions;
/**
* Customize log description using callback.
*/
public function setDescriptionForEvent(Closure $callback): LogOptions;
How can I help you explore Laravel packages today?