1

I used this package called revisionable so I tried to add it to my package.json and run the migration and everything worked fine.

But when I try to create a record and then update them it doesn't fill out the revisions table?

I am using "venturecraft/revisionable": "^1.28", and Laravel 5.5

Here is my code in my model

So here is what I have done in my model

use Venturecraft\Revisionable\Revisionable;
use Venturecraft\Revisionable\RevisionableTrait;

class AnalysisRequest extends Revisionable
{
    use SoftDeletes;
    use RevisionableTrait;

    protected $revisionEnabled = true;
    protected $revisionCleanup = true;
    protected $historyLimit = 100; //Stop tracking revisions after 500 changes have been made.

    protected $dontKeepRevisionOf = array(
        'client_id', 'service_id', 'categories_id', 'methodologies_id', 'current_version', 'due_date'
    );

    protected $keepRevisionOf = array(
        'sample_description',
        'special_instruction',
        'status',
        'rushable'
    );

When did I did wrong?

Can someone shed some light for me. Thanks in advance.

Jaaayz
  • 1,533
  • 7
  • 27
  • 59

2 Answers2

1

For those who uses this package out there. There is no problem with the new versions of Laravel.

The problem is by using the eloquent methods of laravel especially with the update method.

So instead of updating a model like this example below.

$analysis_request = AnalysisRequest::where('id', $input['id'])->update([
            'client_id' => $input['client_id'],
            'sample_description' => $input['sample_description'],
            'special_instruction' => $input['special_instruction'],
            'rushable' => $input['rushable'],
            'status' => 'for_testing'
        ]);

You have to do it this way in order to make revisions of the model. See below

$analysis_request = AnalysisRequest::where('id', $input['id'])->first();
        $analysis_request->client_id = $input['client_id'];
        $analysis_request->sample_description = $input['sample_description'];
        $analysis_request->special_instruction = $input['special_instruction'];
        $analysis_request->status = 'for_testing';
        $analysis_request->save();

As you can see I used the first() method to fetch my model and update it using save() You can't use the save() method if you don't use the first().

Reference link of the issue in github.

I know it's hard to do it like that way. But for now you will be forced to if you want to make your life easier when creating revisions instead of manually doing it on your own. But hey it still depends on your case.

I hope the author makes a fix on this soon in the next release.

Jaaayz
  • 1,533
  • 7
  • 27
  • 59
0

Okay, I did some digging on this, and it looks like it's a limitation of the package (it doesn't include the updating event - ref: vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php).

You can try to "override" the trait function and add the event yourself though:

class AnalysisRequest extends Model {
    use RevisionableTrait {
        bootRevisionableTrait as protected unused;
    }

    ....

    //Override trait function
    public static function bootRevisionableTrait() {
        static::saving(function ($model) {
            $model->preSave();
        });
        static::saved(function ($model) {
            $model->postSave();
        });
        static::created(function($model){
            $model->postCreate();
        });
        static::deleted(function ($model) {
            $model->preSave();
            $model->postDelete();
        });

        //Add in the update events
        static::updating(function ($model) {
            $model->preSave();
        });
        static::updated(function ($model) {
            $model->postSave();
        });

    }
}
JPark
  • 779
  • 4
  • 6