owen-it / laravel-auditing

Record the change log from models in Laravel
https://laravel-auditing.com
MIT License
3.05k stars 390 forks source link

Not logging Affected Relations? #58

Closed bbredewold closed 2 years ago

bbredewold commented 8 years ago

Is it correct that any updated relations are not being logged in the database? I'm using eloquent's sync() function a lot, for updating many-to-many relations...

Any ideas how to do that?

flofloflo commented 6 years ago

@quetzyg As your development on this feature is private, is there something we could do to support you? Maybe you could share your progress and some instructions on what needs to be done?

quetzyg commented 6 years ago

I'm currently not working on this, but I'll probably revisit this soon. I am however, finishing up a new feature based on suggestions made in #395.

ghost commented 6 years ago

This might be of interest: https://github.com/fico7489/laravel-pivot

chelout commented 6 years ago

I wrote package, that covers all types of eloquent relationships, it covers previous link and adds much more. Laravel Relationship Events

OwenMelbz commented 6 years ago

@chelout could you explain how to use your package to make sure this package created audit entries?

chelout commented 6 years ago

@OwenMelbz didn't get you, do you mean, how to use my package with laravel auditing?

OwenMelbz commented 6 years ago

@chelout yes,

For example, I've installed the package, added the several traits to the parent model e.g User

What else? - As when I update the relationships e.g $user->books()->attach(1); it does not create an audit entry.

Are there extra steps required to make it work together to make sure the audit log contains the changes to the relations?

chelout commented 6 years ago

@OwenMelbz for now this packages doesn't work together. My package is just a proposal to solve current issue. It is necessary to implement functionality in laravel auditing in order to use may package.

For my needs i wrote some traits that helps me to store audits for needed events.

AbbyJanke commented 6 years ago

@OwenMelbz fancy seeing you here. The major issue if it hasn't been discussed is Laravel doesn't fire events on pivot changes. I didn't find @chelout's package in my searching but found (https://github.com/fico7489/laravel-pivot) and created a trait that extended Laravel Auditing basically. Even when I added the 'events' that the Laravel Pivot package registers it wouldn't work so i choose a custom trait.

https://gist.github.com/AbbyJanke/9a1615651534a4c7b94f0d3b03144ce7

When i tried using the standard Auditor saving it failed for whatever reason so i created a second table (audits_pivot) and joined them, still working on making the join work so i excluded some code. But you get the idea of it.

AbbyJanke commented 6 years ago

For those interested and @quetzyg if you want to add it to the package here is my full code which is working and tracks the ID of the related model that is being attached/detached.

https://gist.github.com/AbbyJanke/4d245b22dbcec277c207f033f37dae3b

ctf0 commented 6 years ago

here is another version based on @AbbyJanke gist https://gist.github.com/ctf0/fa929221fb801e337ad0c03ac16b27c7

now the remaining part is to add the data from the events to the audit record b4 its created

Update

to get around the issue of saving the relation data along the original audit, you can instead use a timestamp check b4 rending the diff ex.

// revision item date
// foreach($revisions as $rev)
$revision_date = $rev->created_at->toDateTimeString();

// relation item date
// foreach($rev->relations as $rel)
$relation_date = $rel->created_at;

if($relation_date === $revision_date) {
    // render your data
}

but you still have the problem of not able to restore the relation changes because the original audit have no track of them

Update 2

check https://github.com/ctf0/Odin

0528Makoto commented 5 years ago

is compatible for events relationships? chelout/laravel-relationship-events

scheMeZa commented 5 years ago

Not using this package yet, as I'm going to run into this issue and doing research on it. Would like to see this implementation 👍

thitami commented 5 years ago

I find this package of great help and sorted out the triggering of attach, detach and sync events for pivot tables through some research and workarounds;

My current issue is that when I am calling $model->audits I am getting all the entries related to created, updated, etc events but not the entries from event DB with values different than this package's defaults. Any ideas on how I could work around this?

quetzyg commented 5 years ago

@thitami if you've hit a snag, please create a new issue and fill in the template with all the info to reproduce said problem.

thitami commented 5 years ago

Thanks, @quetzyg. Will do shortly.

eumanito commented 5 years ago

@AbbyJanke @ctf0 Are the attributes saved? How I get this?

ctf0 commented 5 years ago

@eumanito yes, check odin

milhouse1337 commented 5 years ago

I think the missing feature to make it work (model events on pivot tables) is now in Laravel 5.8

https://laravel.com/docs/5.8/releases

Intermediate Table / Pivot Model Events In previous versions of Laravel, Eloquent model events were not dispatched when attaching, detaching, or syncing custom intermediate table / "pivot" models of a many-to-many relationship. When using custom intermediate table models in Laravel 5.8, these events will now be dispatched.

Here is the PR: https://github.com/laravel/framework/pull/27571

eumanito commented 5 years ago

@milhouse1337 Thank you for notifying. I will test in my application.

LeonAlvarez commented 5 years ago

@eumanito did you make it work with 5.8 Im lookin to implement this

eumanito commented 5 years ago

@LeonAlvarez not yet. When I do, I will post it here.

kevin1193 commented 5 years ago

Is the pivot observer already implemented in 5.8?

eumanito commented 5 years ago

Yes, @milhouse1337 shared here, take a look.

djdony commented 5 years ago

I think the missing feature to make it work (model events on pivot tables) is now in Laravel 5.8

https://laravel.com/docs/5.8/releases

Intermediate Table / Pivot Model Events In previous versions of Laravel, Eloquent model events were not dispatched when attaching, detaching, or syncing custom intermediate table / "pivot" models of a many-to-many relationship. When using custom intermediate table models in Laravel 5.8, these events will now be dispatched.

Here is the PR: laravel/framework#27571

Can you please explain more how to use with audit?

waska14 commented 5 years ago

Hi. As I understand, it is not yet implemented auditing many to many relations, right?

PizzaTibe commented 5 years ago

Helo I also need this function for a projekt, is there news?

PizzaTibe commented 5 years ago

I found a beter pakage https://altek.gitlab.io/accountant it works out of box for pivot relations super easy and no hacks

eumanito commented 5 years ago

@waska14 I have not implemented it yet

eumanito commented 5 years ago

@PizzaTibe Thanks for contributing, I will test this package.

vpillinger commented 5 years ago

Laravel 5.8 now fires eloquent events if a custom Pivot model exists. Therefore, it should be possible to get pivot audits by

1) Creating a custom pivot model 2) Adding Auditable Trait 3) Append the pivot model audit to the main model(s) audits manually in the model for display purposes.

Maybe this should be added somewhere in the docs because this seems like it is a common question.

rogierborst commented 5 years ago

I can confirm it works in Laravel 5.8. As far as I can tell, your pivot table needs to have its own incrementing id and you need to set public $incrementing = true; on the pivot model. Otherwise you'll run into sql integrity constraint violations, since no auditable_id is passed.

(https://laravel.com/docs/5.8/eloquent-relationships#defining-custom-intermediate-table-models)

amsoell commented 5 years ago

I'm surprised there has been no follow-up here since August, as I'm unable to get this working. It seems like, even if you have $incrementing = true on your pivot model, Laravel doesn't include the model ID, and thus the Auditing package is unable to use it. Has anybody been successful in getting this package to work with custom pivot models?

eumanito commented 5 years ago

I gave up. To work I'm using this package: https://github.com/fico7489/laravel-pivot

amsoell commented 5 years ago

I gave up. To work I'm using this package: https://github.com/fico7489/laravel-pivot

It looks like it was working in version 9.3.1 but then regressed in the latest release. Put an issue up about it, hopefully we can get it working again.

zacharias-pavlatos commented 4 years ago

I can confirm it works in Laravel 5.8. As far as I can tell, your pivot table needs to have its own incrementing id and you need to set public $incrementing = true; on the pivot model. Otherwise you'll run into sql integrity constraint violations, since no auditable_id is passed.

(https://laravel.com/docs/5.8/eloquent-relationships#defining-custom-intermediate-table-models)

Can you provide a gist ?

wrabit commented 4 years ago

@quetzyg do you have any plans on bringing the pivot auditing capability from your package Accountant into this one?

quetzyg commented 4 years ago

@quetzyg do you have any plans on bringing the pivot auditing capability from your package Accountant into this one?

I do not, but you can always use Accountant, instead.

wrabit commented 4 years ago

@quetzyg saving everything for one field change in a table with 30+ columns is overkill for my use-case, especially when 1 or 2 fields change frequently, so I prefer Auditing's flexibility.

YoussefAshraf397 commented 4 years ago

@quetzyg How to fire a relation? I have a translatable model and the $translatedAttributes doesn't affect when making changes in this model.

neylsongularte commented 4 years ago

up

neylsongularte commented 4 years ago

could do optional functionality with Laravel Pivot (https://github.com/fico7489/laravel-pivot)

neylsongularte commented 4 years ago

new Pivot Model Events from laravel not the best style

tmishutin commented 4 years ago

Any update on this idea @quetzyg

quetzyg commented 4 years ago

@tmishutin like I said a few times before, I'm no longer a maintainer of this project. Read the entire thread for suggested alternatives if you require such feature.

MortenDHansen commented 2 years ago

With version 13 we have addressed the oldest issue and now provide a way to solve this. https://www.laravel-auditing.com/docs/13.0/audit-custom