VentureCraft / revisionable

Easily create a revision history for any laravel model
http://twitter.com/duellsy
MIT License
2.55k stars 347 forks source link

Unable to make it work with n-n pivot tables #323

Open realtebo opened 6 years ago

realtebo commented 6 years ago

I cannot figure how log events for records in the pivot table.

I m starting using your package because is so easy. But how can I tell laravel to use your code when using a n to n relation table?

robjuz commented 3 years ago

I ended up with something like this:

You create create your pivot table with an id

Schema::table('post_user', function (Blueprint $table) {
            $table->bigIncrements('id')
            $table->unsignedBigInteger('post_id');
            $table->unsignedBigInteger('user_id');
        });

You create a model class for this

namespace App;

use Eloquent;
use Venturecraft\Revisionable\RevisionableTrait;

class PostUser extends Pivot
{
    use RevisionableTrait;

    public $incrementing = true;

    public $timestamps = false;

    protected $revisionCreationsEnabled = true;

}

Now the most stupid part. You can no longer user $user->attach($post) because this will not trigger the saved event of the PostUser class.

You need to create this entry manually

PostUser::create([
    'user_id' => $user->id,
    'post_id' => $post_id
]);

Looking for better solution :(