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

lastVersion and latestVersion should return the latest and not the first #26

Closed
4unkur opened this issue Sep 14, 2021 · 3 comments
Closed

Comments

@4unkur
Copy link

4unkur commented Sep 14, 2021

The docs are misleading. When I call:

$model->lastVersion;

I am expecting to get the latest version but I get the first. I see in the sources that it returns latest() relationship, so to get the latest version I have to use:

$model->lastVersion()->first();

In this case I get what I expect.
Suggestion: Edit the docs or update the code

@overtrue
Copy link
Owner

Plz, paste your code, because they are same result in laravel:

$model->lastVersion;
$model->lastVersion()->first();

@4unkur
Copy link
Author

4unkur commented Sep 14, 2021

@overtrue I have rechecked and found that issue was due to the missing $model->refresh() call.

Here's the failing test:

    public function it_returns_latest_version()
    {
        $post = Post::create(['title' => 'version1', 'content' => 'version1 content']);
        $firstVersion = $post->lastVersion;

        $post->update(['title' => 'version2']);
        $post->update(['title' => 'version3']);
        $post->update(['title' => 'version4']);

        $this->assertEquals(
            $post->latestVersion()->first()->id,
            $post->lastVersion->id
        );

        $this->assertNotEquals(
            $firstVersion->id,
            $post->lastVersion->id,
        );
    }

The second assertion is failing. In order to fix: $post->refresh() should be executed.
My bad.

Thanks.

@4unkur 4unkur closed this as completed Sep 14, 2021
@overtrue
Copy link
Owner

overtrue commented Sep 14, 2021

Yes, because relations were cached.

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

2 participants