Closed bbredewold closed 2 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?
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.
This might be of interest: https://github.com/fico7489/laravel-pivot
I wrote package, that covers all types of eloquent relationships, it covers previous link and adds much more. Laravel Relationship Events
@chelout could you explain how to use your package to make sure this package created audit entries?
@OwenMelbz didn't get you, do you mean, how to use my package with laravel auditing?
@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?
@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.
@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.
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
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
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
is compatible for events relationships? chelout/laravel-relationship-events
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 👍
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?
@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.
Thanks, @quetzyg. Will do shortly.
@AbbyJanke @ctf0 Are the attributes saved? How I get this?
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
@milhouse1337 Thank you for notifying. I will test in my application.
@eumanito did you make it work with 5.8 Im lookin to implement this
@LeonAlvarez not yet. When I do, I will post it here.
Is the pivot observer already implemented in 5.8?
Yes, @milhouse1337 shared here, take a look.
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?
Hi. As I understand, it is not yet implemented auditing many to many relations, right?
Helo I also need this function for a projekt, is there news?
I found a beter pakage https://altek.gitlab.io/accountant it works out of box for pivot relations super easy and no hacks
@waska14 I have not implemented it yet
@PizzaTibe Thanks for contributing, I will test this package.
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.
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)
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?
I gave up. To work I'm using this package: https://github.com/fico7489/laravel-pivot
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.
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 ?
@quetzyg do you have any plans on bringing the pivot auditing capability from your package Accountant into this one?
@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.
@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.
@quetzyg How to fire a relation? I have a translatable model and the $translatedAttributes doesn't affect when making changes in this model.
up
could do optional functionality with Laravel Pivot (https://github.com/fico7489/laravel-pivot)
new Pivot Model Events from laravel not the best style
Any update on this idea @quetzyg
@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.
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
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?