E52E [12.x] Query Builder: add support for composite where-in clauses via whereRowIn by Sadhinrana · Pull Request #58501 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@Sadhinrana
Copy link

This PR adds support for composite WHERE IN clauses to the query builder via a new whereRowIn API.

Motivation

Many databases (MySQL, PostgreSQL, SQLite, MariaDB) support row value expressions such as:

(col1, col2) IN ((v1, v2), (v3, v4))

Laravel’s query builder currently does not expose a native way to express this pattern, requiring raw queries or manual condition grouping. This PR introduces a first-class, expressive API for this use case.

New API

$query->whereRowIn(['id', 'name'], [
    [1, 'Alice'],
    [2, 'Bob'],
]);

$query->whereNotRowIn(['id', 'name'], $subquery);
$query->orWhereRowIn(['col1', 'col2'], $values);

Implementation details

  • Uses row constructor syntax for databases that support it
  • Provides a SQL Server fallback by expanding to OR / AND conditions
  • Supports subqueries, raw expressions, and boolean variants
  • Validates row shape to ensure column/value consistency
  • Follows existing whereIn binding and grammar conventions

Database support

  • ✅ MySQL
  • ✅ MariaDB
  • ✅ PostgreSQL
  • ✅ SQLite
  • ✅ SQL Server (fallback syntax)

Tests
Includes comprehensive query builder tests covering:

  • All supported database grammars
  • IN / NOT IN and boolean variants
  • Subqueries and raw expressions
  • Single-column and multi-column cases
  • Invalid input handling

@marius-ciclistu
Copy link
marius-ciclistu commented Jan 25, 2026

Tuples could be used for composite primary keys models collection deserialization. But tuples are also slower from my knowledge.

@cosmastech
Copy link
Contributor

Previous attempt: #56509

@Sadhinrana
Copy link
Author

Previous attempt: #56509

Thanks for sharing.

@Sadhinrana
Copy link
Author

Tuples could be used for composite primary keys models collection deserialization. But tuples are also slower from my knowledge.

Good point. This PR is focused on exposing a user-facing query builder API that mirrors native row value expression support in supported databases.

For SQL Server, a non-tuple fallback is already used. For other databases, this follows the database-native syntax and query planner behavior. Performance trade-offs would likely depend on engine, index configuration, and use case.

Using this for composite key hydration could be interesting, but I agree that would require separate discussion and benchmarking before being used internally.

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.

3 participants

0