[13.x] Add insertReturning method query builder - #61286
Open
TABbl wants to merge 2 commits into
Open
Conversation
TABbl
force-pushed
the
feature/add-insert-returning-to-query-builder
branch
from
August 22, 2026 11:13
9d846c7 to
c6a2e2f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds an insertReturning() method to Illuminate\Database\Query\Builder. Laravel already has insertOrIgnoreReturning() (#59025), which returns inserted rows' columns via native RETURNING — this PR adds the plain counterpart of it: same idea, minus the conflict-ignoring behavior, for cases where you just want a normal batch insert with the results back.
insertOrIgnoreReturning() solved returning IDs for inserts that silently skip conflicting rows. But insertOrIgnore is the wrong tool whenever an application actually wants constraint violations to fail loudly — which is the common case outside of idempotent import/sync jobs. Today, there is no way to batch-insert several rows and get their generated results back in one round trip while still preserving normal insert failure behavior:
insertGetId() only supports a single row — looping it per row for a batch defeats the purpose of batching and multiplies query count linearly with row count.
insert() returns only a boolean.
The only current workaround is dropping to DB::select(DB::raw(...)) with hand-written SQL.