Laravel-activitylog: Use additional table fields

Created on 19 Nov 2018  Â·  12Comments  Â·  Source: spatie/laravel-activitylog

I need another field in the table customer_id that is the customer of the causer_id. In my application, all users belong to a customer and it would be faster to query a column customer_id for the customer level and causer_id for the user level.

Is this possible?

enhancement question

Most helpful comment

Ok, thanks for clarifying. Instead of adding a second parameter to log we could introduce a new method right?

activity()
->with(function(Activity $activity) { $activity->customer_id = $activity->causer->customer->getKey(); })
->log('message');

That with name could be improved. Maybe tap? 😄

All 12 comments

My first idea would be an observer for the Activity model which adds the customer_id to the custom_properties or you customize the activity_model config and set the customer-id on a new column added to your own model class.
https://laravel.com/docs/5.7/eloquent#observers

This will work for you, since activity() returns eloquent model you can do this:

$activity = activity()->log('test');
$activity->customer_id = 5;
$activity->save();

It probably could, but that requires 3 lines of code; if it was built into the package, it could be incorporated into 1 line of code.

An idea would be an optional callback function as second argument for the log() method.
@freekmurze what do you think?

activity()->log('message', function(Activity $activity) { $activity->customer_id = $activity->causer->customer->getKey(); });

But this would only be a solution for manual logs. So I still think that an observer is the best solution.

That would be great; I only do manual logs anyway.

Best regards,

Tracy Barnes
You Got Net, LLC

Main: (903) 862-2152
Fax: (469) 828-6810

[email protected]tbarnes@yougotnet.com
http://www.yougotnet.comhttp://www.yougotnet.com/

Web Design, Web Hosting, Search Engine Optimization
eCommere, Domain Name Registration

On November 23, 2018 at 7:29:19 AM, Tom Witkowski ([email protected]notifications@github.com) wrote:

An idea would be an optional callback function as second argument for the log() method.
@freekmurzehttps://github.com/freekmurze what do you think?

activity()->log('message', function(Activity $activity) { $activity->customer_id = $activity->causer->customer->getKey(); });

But this would only be a solution for manual logs. So I still think that an observer is the best solution.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com/spatie/laravel-activitylog/issues/462#issuecomment-441241489, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AC0WjoRkj_Ucl2iNlw9UJC-HC_dwp6lbks5ux_gsgaJpZM4Yps7c.

@Gummibeer I don't think we'd need to support that callback as a second argument. You can do this:

tap(activity()->log('test'), $callback) {
   // do stuff here...
}

That seems you are saving the log first and then after it is saved, then do more stuff. Just seems like adding a band aid and not really addressing the issue.

It is common to want to add additional fields into the activity log; I'm not seeing the difficulty in adding it to the tool so that it can be more streamlined and efficient.

@freekmurze like @yougotnet said: this will save the model twice - something we should prevent for logs. For me the table/model shouldn't even allow an update, but this is something else.

Ok, thanks for clarifying. Instead of adding a second parameter to log we could introduce a new method right?

activity()
->with(function(Activity $activity) { $activity->customer_id = $activity->causer->customer->getKey(); })
->log('message');

That with name could be improved. Maybe tap? 😄

Sounds great! That would allow any number of additional fields for the model.

I've added the discussed tap() method in #472 - everyone is welcome to test it and/or give a review.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

damosse31 picture damosse31  Â·  5Comments

BerendSpigt picture BerendSpigt  Â·  4Comments

lucianobosco picture lucianobosco  Â·  5Comments

ekandreas picture ekandreas  Â·  3Comments

hackel picture hackel  Â·  5Comments