Skip to content

[12.x] Resolve issue with BelongsToManyRelationship factory #55608

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

Merged
merged 2 commits into from
Apr 30, 2025

Conversation

jackbayliss
Copy link
Contributor

@jackbayliss jackbayliss commented Apr 30, 2025

When updating to 12.11.0, I noticed a bunch of tests started to fail with :

BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::()

This looks to be have been introduced via #55558

This is because :

$relationship = $model->{$this->relationship}();

If it's not an instance of Factory, this relationship can be an empty string-- thus causing this to fail as there's no function to call. The $relationship var is only used if it's a factory instance anyway, so this makes sense- I hope!

Here's a failing test as a rough example:

    #[DataProvider('providerExample')]
    public function test_belongs_to_many_relationship_empty_factory(int $relationshipTotal)
    {
        $relation = FactoryTestRoleFactory::times($relationshipTotal)->create();
        
        FactoryTestUserFactory::times(2)
            ->hasAttached($relation)
            ->create();

        $this->assertCount(2, FactoryTestUser::all());
    }

    public static function providerExample()
    {
        return [
            [0], // maybe I want to assert something if there's no relation
            [1],
            [2]
        ];
    }

Cheers 🫡

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@jackbayliss jackbayliss marked this pull request as ready for review April 30, 2025 09:11
@taylorotwell taylorotwell merged commit 5b0d72c into laravel:12.x Apr 30, 2025
58 checks passed
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

Successfully merging this pull request may close these issues.

2 participants