Skip to content

[13.x] Use the write connection in BelongsToMany createOrFirst lookup - #62223

Merged
taylorotwell merged 1 commit into
laravel:13.xfrom
xurshudyan:belongs-to-many-create-or-first-write-pdo
Oct 28, 2026
Merged

taylorotwell merged 1 commit into
laravel:13.xfrom
xurshudyan:belongs-to-many-create-or-first-write-pdo

Conversation

@xurshudyan

Copy link
Copy Markdown
Contributor

After a unique constraint violation, BelongsToMany::createOrFirst() looks up the existing record on the read connection. With a lagging replica it isn't found and the exception is rethrown:

$post->tags()->firstOrCreate(['slug' => 'laravel']);

// UniqueConstraintViolationException

The other createOrFirst() implementations already use the write connection since #48161.

@taylorotwell
taylorotwell merged commit de027ed into laravel:13.x Oct 28, 2026
64 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