Activity gets logged twice #754
-
So my activity is getting logged twice on the belongsTo relationship. I'm using laravel backpack. To explain further. For example I have a User and a User Type model. So User will have a user_type_id as field. So from my understanding the relationship should be User belongsTo UserType since User is the one that has the foreign key of User Type. So i have this code on my App\User.php file: public function user(){
return $this->belongsTo('App\UserType', 'user_type_id', 'id');
} Then I have the logging code on App\Models\BackpackUser.php public function save(array $options = [])
{
if (empty($this->id)) {
parent::save($options);
activity('user')
->causedBy($this)
->performedOn(backpack_user())
->log('created');
} else {
parent::save($options);
activity('user')
->causedBy($this)
->performedOn(backpack_user())
->log('updated');
}
} And when I create/edit User, the activity gets logged twice. All field has the same values, even the timestamps. But when I change the relationship to hasOne, it only gets logged once. Am I doing something wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hey, do you use the package traits on any model? I don't know backpack, but could it be that backpack uses the activitylog by default? And your custom log is the duplicate? Your
The |
Beta Was this translation helpful? Give feedback.
Hey,
do you use the package traits on any model?
https://docs.spatie.be/laravel-activitylog/v3/advanced-usage/logging-model-events/
I highly recommend to do so, because they keep track on model events by default and will create a log automatically.
I don't know backpack, but could it be that backpack uses the activitylog by default? And your custom log is the duplicate?
Your
belongsTo
relationship is right - the model with the foreign ID in it's own table is the one that belongs to the model with only its primary ID.https://laravel.com/docs/7.x/eloquent-relationships#one-to-many
The
User
model belongs to theUserType
model and the…