Skip to content

[12.x] Allow Factory connection method to accept null - #58108

Merged
taylorotwell merged 1 commit into
laravel:12.xfrom
jackbayliss:12.x-allow-connection-null
Dec 12, 2025
Merged

taylorotwell merged 1 commit into
laravel:12.xfrom
jackbayliss:12.x-allow-connection-null

Conversation

@jackbayliss

@jackbayliss jackbayliss commented Dec 12, 2025 •

Copy link
Copy Markdown
Contributor

In some of my factories I pass in the connection such as:

Product::connection('replica-1')->factory()->create(); 

However, the definition references another factory ie:

    public function definition(): array
    {
        return [
            'user_id'  => User::factory(),
        ];
    }

I want to pass the connection through ie User::factory()->connection($this->connection)

but, the connection can be null - as i'm not setting a connection in every test so I end up having to do:

// Before
    public function definition(): array
    {
        return [
             'user_id'  => $this->connection ? User::factory()->connection($this->connection) : User::factory()
        ];
    }

// After
    public function definition(): array
    {
        return [
             'user_id'  => User::factory()->connection($this->connection);
        ];
    }

This is just a bit of DX, as the connection property can be null.

Thanks

@github-actions

Copy link
Copy Markdown

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 changed the title [12.x[ Allow Factory connection method to allow null [12.x] Allow Factory connection method to allow null Dec 12, 2025
@jackbayliss jackbayliss changed the title [12.x] Allow Factory connection method to allow null [12.x] Allow Factory connection method to accept null Dec 12, 2025
@jackbayliss
jackbayliss marked this pull request as ready for review December 12, 2025 15:01
@taylorotwell
taylorotwell merged commit 7505152 into laravel:12.x Dec 12, 2025
72 checks passed
akyrey pushed a commit to akyrey/framework that referenced this pull request Dec 29, 2025
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