Skip to content

Conversation

@jackbayliss
Copy link
Contributor

@jackbayliss jackbayliss commented Dec 12, 2025

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

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
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