Hi everyone.
Is there a way to log the causer in the description of the log?
I can read from the documentation that you can override the description using:
public function getDescriptionForEvent(string $eventName): string
{
return "This model has been {$eventName}";
}
Can I insert the causer in this description?
This is my model (just for reference):
class DiaryPage extends Model
{
use LogsActivity;
protected $fillable = [
'id',
'date',
'description',
'author',
'diary',
'user_id',
];
public function user()
{
return $this->belongsTo('App\User');
}
public function diary()
{
return $this->belongsTo('App\Diary','diary','progressive_number');
}
//Log events
protected static $logName = 'Diary';
protected static $logFillable = true;
protected static $logOnlyDirty = true;
protected static $submitEmptyLogs = false;
public function getDescriptionForEvent(string $eventName): string
{
return "An element has been {$eventName}";
}
}
I want something like "An element has been {$eventName} by "$causer".
Thank you!
The way you describe it doesn't work.
But you have multiple ways to solve it.
You can use the tapActivity() method to edit/set the description. There you have access to the whole model and activity right before saving.
You can use translations with placeholders and fill in the dynamic data during rendering.
And I bet that there are more ways to get it done. 馃槉
Hi Gummibeer and thank you for your answer!
I can see from the documentation that I should register activity in this way:
activity()
->performedOn($anEloquentModel)
->causedBy($user)
->withProperties(['customProperty' => 'customValue'])
->log('Look mum, I logged something');
My question is: where should I put this code?
Using the Model Logger I put the code directly in my model, but using the activity which is the best place?
Hey,
I've thought that you want to do it in the models. Or is this in addition?
But for the custom logs you can define everything via the methods. So for sure you can also use the tap() method but you can also pass the final description into the log() method. There isn't a global place to run an action for all activities. So you will have to write any custom wrapper for this.
I'm sorry @Gummibeer
tapActivity() was exactly was I was searching for!
I just wasn't able to use it. Now I have what I need.
Really thank you for your support!
No problem and happy to help. :)