Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not logging Affected Relations? #58

Closed
bbredewold opened this issue Jun 1, 2016 · 96 comments
Closed

Not logging Affected Relations? #58

bbredewold opened this issue Jun 1, 2016 · 96 comments
Labels
enhancement An improvement or new feature help wanted Assistance or contributions needed

Comments

@bbredewold
Copy link

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?

@camih
Copy link
Contributor

camih commented Jun 6, 2016

The auditing currently uses events, but in case of pivots, there are no events being triggered. There are many issues reported regarding this. See for example here: laravel/framework#8452 .
I also need this and currently did not find a very nice way to handle it.

@robclancy
Copy link

Can you not just override those methods like sync, attach and detach? Or even better would be a chain on the relationship in some way, will explain more if I come up with a way of doing this. Trying out this package and I think we will need it to work for this and we can work around it easy enough, if a workaround is good enough we will make a PR.

@anteriovieira
Copy link
Member

Sorry for the delay.

Hello @robclancy , let's think of something. I'll keep this open to give continuity to this improvement.

@anteriovieira anteriovieira added the enhancement An improvement or new feature label Aug 6, 2016
@fly-studio
Copy link

yes, 'sync attach detach' donot log,
4.x support it?

@quetzyg
Copy link
Contributor

quetzyg commented Dec 29, 2016

@fly-studio, as soon as version 4.0 is out the door, we'll try to work on many-to-many audit support for 4.1.

@projct1
Copy link

projct1 commented Jan 15, 2017

When will release 4 version? 😃

@quetzyg
Copy link
Contributor

quetzyg commented Jan 15, 2017

@rorc, when it's done (i.e. when I finish adding tests)

Meanwhile, you already can use "owen-it/laravel-auditing": "4.0.x-dev" in your composer.json.
The v4 documentation is available here.

@projct1
Copy link

projct1 commented Jan 15, 2017

@quetzyg Ok, thanks 😄

@jschlies
Copy link

I strongly vote for this feature. I'm currently working on a custom auditor that would record (to a seperate table) the foreign key relationships relations to a separate table, then I extend $Model->getAuditArr() to pull it all together the way we want. Lots of DB hits and in general, not a perfect solution. Will switch off this when this feature is implemented.

@projct1
Copy link

projct1 commented Mar 18, 2017

How progress here? 😀

@od3n
Copy link

od3n commented Apr 8, 2017

+1 still waiting for the best solution

@ghost
Copy link

ghost commented Apr 12, 2017

considering using this package but I would need pivot auditing

@neylsongularte
Copy link

tenho o mesmo problema

@neylsongularte
Copy link

neylsongularte commented May 11, 2017

Experimental package for events in many to many relations:

https://github.com/neylsongularte/eloquent-extra-events

My temporary solution:

// Add on your AppServiceProvider
$saveManyToManyAudit = function ($event, $eventData) {
            $audit = new Audit;
            $audit->user_id = Auth::check() ? Auth::user()->getAuthIdentifier() : null;
            $audit->event = $event;
            $audit->auditable_id = $eventData['parent_id'];
            $audit->auditable_type = $eventData['parent_model'];
            $audit->old_values = [];
            unset($eventData['parent_id']);
            unset($eventData['parent_model']);
            $audit->new_values = $eventData;
            $audit->url = App::runningInConsole() ? 'console' : Request::fullUrl();
            $audit->ip_address = Request::ip();
            $audit->user_agent = Request::header('User-Agent');
            $audit->created_at = Carbon::now();
            $audit->save();
        };

        $this->app['events']->listen('eloquent.synced*', function ($eventData) use ($saveManyToManyAudit) {
            if($eventData['changes']['attached'] || $eventData['changes']['detached'] || $eventData['changes']['updated']) {
                $saveManyToManyAudit('synced', $eventData);
            }
        });

        $this->app['events']->listen('eloquent.attached*', function ($eventData) use ($saveManyToManyAudit) {
            $saveManyToManyAudit('attached', $eventData);
        });

        $this->app['events']->listen('eloquent.detached*', function ($eventData) use ($saveManyToManyAudit) {
            $saveManyToManyAudit('detached', $eventData);
        });

@anteriovieira

@neylsongularte
Copy link

laravel/framework#14988

@TheAlexLichter
Copy link

Any update concerning this problem?

@quetzyg
Copy link
Contributor

quetzyg commented Jun 5, 2017

Hi everyone,

I've been thinking in a few ways to achieve this, and I'm inclided to leave it up to the developer to implement part of the functionality.

The package can already handle other events besides created, updated,deleted and restored, and like Taylor suggests, the attach/detach/sync logic should be handled in a repository, in which the proper event is fired after the DB operation happens.

So, the idea is to add three new AuditableObserver methods: attached(), detached() and synced()

The developer would be responsible for firing/dispatching said events and pass the data they wish to audit/log.

From that point on, the appropriate AuditableObserver method would kick in and handle things.

I think this brings a good balance between new feature and code complexity.

Thoughts?

@TheAlexLichter
Copy link

TheAlexLichter commented Jun 5, 2017

@quetzyg Sounds good to me! Because there won't be an official support concerning the events, I think that's a very good way, especially because the developers are now "unrestricted".

But please provide an example for the implementation in the docs 😛

@quetzyg
Copy link
Contributor

quetzyg commented Jun 5, 2017

@manniL, yeah, the idea is to keep it flexible, because I have no idea how developers want to store changes, specially when we're talking about sync events, which may affect more than one row in a pivot table. Do they want to create an audit with all the row changes or do they want to audit each row individually? There's no one-size-fits-all in this case.

And don't worry, the docs will be updated once this gets implemented.

@TheAlexLichter
Copy link

@quetzyg That's a good plan!

Well, in my case I'd simply store the changes (e.g. User A Was linked to posts 1,2,3 and is now linked to Post 4,6,7, I'd just change the stored numbers). If the relation models itself were changed, it'd be fine if there would be an extra audit model for that.

@quetzyg
Copy link
Contributor

quetzyg commented Jun 5, 2017

For a simple case like the one you described it's fine to use just one audit record, but when you have extra data in the pivot or when you detach hundreds of relations in one go, things would get out of hand very quickly and stuffing all that data in just one record would make it hard to track changes.

But again, I'll leave that decision to the developer.

@TheAlexLichter
Copy link

@quetzyg Yes, this can become very hairy in situations like the one you described. But as the developers are flexible, everyone will adapt it to their needs :)

@jschlies
Copy link

jschlies commented Jun 5, 2017

I think I have a solution for issue. Are you accepting pull requests??? It has, I admin, some of the data issues stated above. I can have it done in acceptable form in a couple of Saturdays

@quetzyg
Copy link
Contributor

quetzyg commented Jun 5, 2017

@jschlies, sure. Feel free to send a PR and we'll review it.

@TheAlexLichter
Copy link

TheAlexLichter commented Jun 5, 2017

I'm very curious! Good luck @jschlies and hit me up when u need help :)

@neylsongularte
Copy link

👍

@kevin1193
Copy link

Is the pivot observer already implemented in 5.8?

@eumanito
Copy link

Yes, @milhouse1337 shared here, take a look.

@djdony
Copy link

djdony commented Mar 21, 2019

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
Copy link

waska14 commented Apr 6, 2019

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

@PizzaTibe
Copy link

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

@PizzaTibe
Copy link

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

@eumanito
Copy link

@waska14 I have not implemented it yet

@eumanito
Copy link

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

@vpillinger
Copy link

vpillinger commented Apr 25, 2019

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
Copy link

rogierborst commented Aug 8, 2019

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
Copy link

amsoell commented Oct 29, 2019

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
Copy link

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

@amsoell
Copy link

amsoell commented Oct 29, 2019

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
Copy link

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
Copy link

wrabit commented Feb 10, 2020

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

@quetzyg
Copy link
Contributor

quetzyg commented Feb 10, 2020

@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
Copy link

wrabit commented Feb 11, 2020

@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
Copy link

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

@neylsongularte
Copy link

up

@neylsongularte
Copy link

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

@neylsongularte
Copy link

new Pivot Model Events from laravel not the best style

@tmishutin
Copy link

Any update on this idea @quetzyg

@quetzyg
Copy link
Contributor

quetzyg commented Jun 27, 2020

@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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An improvement or new feature help wanted Assistance or contributions needed
Projects
None yet
Development

No branches or pull requests