-
Hello, I take advantage of the LogsActivity trait to automatically log when creating models. But I have different places where I create new Model and I would like to set a different logName depending on that. public function fromMethodA () {
activity("create-from-a");
MyModel::create(.....);
}
public function fromMethodB () {
activity("create-from-b");
MyModel::create(.....);
} So far I tried to set |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey, you could define a Another option would be $default = config('activitylog.default_log_name');
config('activitylog.default_log_name', 'my_new_log_name');
$model = MyModel::create(...);
config('activitylog.default_log_name', default');
return $model; You could make this more readable and reusable by adding a macro to the ActivityLogger::macro('withLogName', function(string $logName, Closure $callback) {
$defaultLogName = config('activitylog.default_log_name');
config('activitylog.default_log_name', $logName');
$result = $callback();
config('activitylog.default_log_name', default');
return $result;
}); And you could use it like the following: $model = ActivityLogger::withLogName('my_new_log_name', function() {
return MyModel::create(...);
}); |
Beta Was this translation helpful? Give feedback.
Hey, you could define a
tapActivity()
method on your model which can change the activity before it's persisted. But you need some fancy way to transfer the calling method to this tap method.Without any container usage or similar global variable holder you could use
debug_backtrace()
for example.Another option would be
You could make this more readable and reusable by adding a macro to the
ActivityLogger
: