Laravel audit log for model events using generic way - laravel

I am using Laravel 5.2.
I want to create audit log entries for each and every model in my project. Whenever any model gets created, updated or deleted; the audit log for that record should be logged along with old and new values.
For example if I update user's first name from John to George, the audit table entry should be logged something like First name updated from John to George.
I know I can fire created, updated and deleted events and using individual listners I can log this. But I want to create it more generic way.
I don't want to define boot method with created, updated and deleted events in each and every model as I have more than 75 models existing in my project. I don't like duplication of that method in each and every model. I want to make it more generic way, like use something like trait, using that in model should automatically identify which event is fired.
Again, the listener will also listen the event but not for any specific model but generic. I don't want any tight coupling. It should decide which model is updated and make entry of log according to that.
Can any one guide me how to achieve this?

Related

Custom Views for Case Records in Dynamics 365

I have a Case View which shows all the records that were created by the me (My Cases view). I now want to all those Cases edited/modified by me to be visible in that view. The problem is those modified by me could be later modified by the system when a workflow updates the Case, which changes the Modified By User data. Thus, not allowing me to use Modified By (Current User) as a condition for the view.
Is there any other way or condition which I can use to make sure the I can accomplish this?
Thank you in advance.
You may create a new Entity with One to Many relationship with Case Entity. Whenever there is Insert or Update you can add a new record in the mapping entity with Modified By Information and Created On OOB.
You can create a view in the new entity with record modified by Me.
P.S - Single case record will be edited multiple times with multiple users so it will result in high volume of data and may want to look for archiving strategy.

How to store log of all changes to Eloquent model in Laravel 5.4?

I want to store changes to all fields in Eloquent model into database.
I can do it using created and updated events but there is a problem with multiple foreign relations (described as separate tables).
Example:
User
login
Roles -> hasMany
When I update login field it is easy to write old and new value into database, but when I update Roles relation nothing happens.
How can I track all foreign relations (like hasMany, hasManyThrough, ManyToMany)?
Owen-it has a very nice Library called laravel-auditing, which keeps an easy to query list of all changes that are made to a model, and I think it does quite an awesome piece of work. Have used it and it is worth it to try out.
There is no embedded and simple method to do it.
Eloquent doesn't provide any method to implement observers on related models by design. Many proposal in this way have been rejected by Taylor (just one example).
The only thing you can do, is to create your own methods to do it.
You have many possibilities, here are some of them in order of complexity (some of them are "dirty" :-)
add a created and updated observer on each related model
override the save() or create your own saveAndFire() method on your eloquent instances, and from that method retrieve the parent and call its log methods before saving. (this is a little bit "dirty" imho)
encapsulate all your persistence layer and fire events yourself on saving objects (look at the repository pattern, for example)

How to log all events in my Laravel application

I would like to log all events in my app like:
User 1 - created a new item - created_at...
User 2 - deleted item 213- created_at...
etc.
I don't really see how am I supposed to do it.
Is there any plugin that should help?
You should use Eloquent Events for this. These events will fire before or after any data will be saved into or restored from a database.
Eloquent models fire several events, allowing you to hook into various
points in the model's lifecycle using the following methods: creating,
created, updating, updated, saving, saved, deleting, deleted,
restoring, restored. Events allow you to easily execute code each time
a specific model class is saved or updated in the database.

Laravel 5 - what's the difference for creating and created model events?

Sorry I am new to Laravel and confused a little bit about the difference of creating and created model events. I found in laravel docs:
Eloquent models fire several events, allowing you to hook into various
points in the model's lifecycle using the following methods: creating,
created, updating, updated, saving, saved, deleting, deleted,
restoring, restored...Whenever a new model is saved for the first
time, the creating and created events will fire.
But what's the difference of creating and created events? Will creating and created events always be fired together? Or is there a situation when creating is fired but created is not?
The main difference (at least for me) is that:
The creating event is more "powerful" because as the example of the docs states, you're able to cancel the creation of a model during the creating event, if, for example, it's not valid.
On the other hand, the created event will be fired when the model is already saved to the database so you're not able to cancel anything, you only could prepare other data, for example, once the model is saved.
A possible case when one event is fired, but not the other: in the example of the docs, if the model is not valid, the creating event will be fired, but not the created event as it's not valid, and it won't be saved to the database.
Greetings!
Creating is before Created so you won't have access to the the model id.

MS CRM 4 - Custom entity with "regardingobjectid" functionality

I've made a custom entity that will work as an data modification audit (any entity modified will trigger creating an instance of this entity). So far I have the plugin working fine (tracking old and new versions of properties changed).
I'd like to also keep track of what entity this is related to. At first I added a N:1 from DataHistory to Task (eg.) and I can indeed link back to the original task (via a "new_tasksid" attribute I added to DataHistory).
The problem is every entity I want to log will need a separate attribute id (and an additional entry in the form!)
Looking at how phone, task, etc utilize a "regardingobjectid", this is what I should do. Unfortunately, when I try to add a "dataobjectid" and map it to eg Task and PhoneCall, it complains (on the second save), that the reference needs to be unique. How does the CRM get around this and can I emulate it?
You could create your generic "dataobjectid" field, but make it a text field and store the guid of the object there. You would lose the native grids for looking at the audit records, and you wouldn't be able to join these entities through advanced find, fetch or query expressions, but if that's not important, then you can whip up an ASPX page that displays the audit logs for that record in whatever format you choose and avoid making new relationships for every entity you want to audit.
CRM has a special lookup type that can lookup to many entity types. That functionality isn't available to us customizers, unfortunately. Your best bet is to add each relationship that could be regarding and hide the lookups that aren't in use for this particular entity.

Resources