Skip to content

[13.x] Add insertReturning method query builder - #61286

Open
TABbl wants to merge 2 commits into
laravel:13.xfrom
TABbl:feature/add-insert-returning-to-query-builder
Open

[13.x] Add insertReturning method query builder#61286
TABbl wants to merge 2 commits into
laravel:13.xfrom
TABbl:feature/add-insert-returning-to-query-builder

Conversation

@TABbl

@TABbl TABbl commented Aug 22, 2026

Copy link
Copy Markdown

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.

@TABbl TABbl changed the title Add insertReturning method query builder [13.x] Add insertReturning method query builder Aug 22, 2026
@TABbl
TABbl force-pushed the feature/add-insert-returning-to-query-builder branch from 9d846c7 to c6a2e2f Compare August 22, 2026 11:13
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.

1 participant