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

Create Initial versions of model. #51

Closed
mansoorkhan96 opened this issue Mar 1, 2024 · 0 comments
Closed

Create Initial versions of model. #51

mansoorkhan96 opened this issue Mar 1, 2024 · 0 comments

Comments

@mansoorkhan96
Copy link
Contributor

Thanks for amazing work on this really hand package ❤️

I found an issue with version keeping, so here is an explaination.

Problem

When saving a Model for first time, It does not save the existing state of the model, instead it starts saving the new state of the model.

Steps to reproduce

Assuming, we have a Model with existing data:

$existingPost = [
  'id' => 1,
  'title' => 'Existing Title',
];

Update the title of model

$post->update([
  'title' => 'New Title'
]);

Expected behaviour:

We expect a version with title = Existing Title and optionally another version with title = New Title which matches the state of original/source Model.

Actual behaviour:

We are losing a version where title = Existing Title.

Possible Solution

It should create an initial version of the model first and then start saving the new versions.

Work around

I have a single Model called Post which uses the Versionable trait. So I created a console command SetupVersionable to create the initial versions of the Post model, before writers/users start changing the content.

use Overtrue\LaravelVersionable\Version;

class SetupVersionable extends Command
{
    protected $signature = 'app:setup-versionable';

  
    public function handle()
    {
        Post::all()
            ->each(fn (Post $post) => Version::createForModel($post, $post->only($post->versionable)));
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant