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;
##Options methods
##defaults
public static function defaults(): LogOptions;
##logAll
This method is equivalent to ->logOnly(['*']).
public function logAll(): LogOptions;
##logUnguarded
public function logUnguarded(): LogOptions;
This can be combined with ->logFillable() or ->logOnly(). The final set of logged attributes is the union of all sources.
##logFillable
public function logFillable(): LogOptions;
This can be combined with ->logUnguarded() or ->logOnly(). The final set of logged attributes is the union of all sources.
##dontLogFillable
public function dontLogFillable(): LogOptions;
##logOnlyDirty
public function logOnlyDirty(): LogOptions;
##logOnly
public function logOnly(array $attributes): LogOptions;
##logExcept
Convenient method for excluding specific attributes from logging.
public function logExcept(array $attributes): LogOptions;
##dontLogIfAttributesChangedOnly
public function dontLogIfAttributesChangedOnly(array $attributes): LogOptions;
##dontLogEmptyChanges
public function dontLogEmptyChanges(): LogOptions;
##logEmptyChanges
public function logEmptyChanges(): LogOptions;
##useLogName
public function useLogName(string $logName): LogOptions;
##useAttributeRawValues
public function useAttributeRawValues(array $attributes): LogOptions;
##setDescriptionForEvent
public function setDescriptionForEvent(Closure $callback): LogOptions;