whereIn compiles to a SQL IN list with one bound placeholder per array element. You can see exactly what it builds with toSql() and getBindings(), and you should, because two of its behaviours will surprise you:
$q = DB::table('orders')->whereIn('status', ['pending', 'shipped']);
echo $q->toSql(); // select * from "orders" where "status" in (?, ?)
echo json_encode($q->getBindings()); // ["pending","shipped"]
echo $q->count(); // 3
whereNotIn is the same shape with not in, and a closure gives you a subquery instead of a value list:
DB::table('orders')->whereNotIn('status', ['pending', 'shipped']);
// select * from "orders" where "status" not in (?, ?) bindings: ["pending","shipped"] → 1 row
DB::table('orders')->whereIn('customer_id', function ($sub) {
$sub->select('customer_id')->from('orders')->where('total', '>=', 300);
});
// select * from "orders" where "customer_id" in
// (select "customer_id" from "orders" where "total" >= ?) bindings: [300] → 2 rows
Note the binding count in the subquery form: one. The IDs never leave the database. That difference matters below.
Trap 1: the empty array, and the destructive half of it
An empty array does not produce “no condition”. Laravel substitutes a constant, and the constant is different for the two methods:
DB::table('orders')->whereIn('status', []);
// select * from "orders" where 0 = 1 → 0 rows
DB::table('orders')->whereNotIn('status', []);
// select * from "orders" where 1 = 1 → 4 rows (the whole table)
Both are logically correct — nothing is a member of the empty set, and everything is a non-member — but the asymmetry is dangerous. A whereIn that silently returns nothing shows up as an empty list in the UI. A whereNotIn that silently matches everything shows up as data loss. This is a real delete, run against a scratch table:
$keep = []; // an upstream filter matched nothing
$q = DB::table('hw_wi_keep')->whereNotIn('id', $keep);
// delete from "hw_wi_keep" where 1 = 1
rows before: 5
deleted: 5
rows after: 0
Most whereIn tutorials do not mention the empty array at all. Guard the array before you build the query, and do it at the top of the method rather than inside the query chain:
if (empty($ids)) {
return collect(); // or: abort(422), or skip the delete entirely
}
Order::whereNotIn('id', $ids)->delete();
The same reasoning applies to soft deletes: delete() on a model using the SoftDeletes trait writes a timestamp rather than removing rows, which changes what “data loss” means here but not whether it happened — see Laravel soft deletes.
Trap 2: the placeholder ceiling is a real number, and it is lower than you think
Laravel doesn’t impose a strict limit; your database might. Here are the actual limits, found by bisecting:
-- SQLite 3.53.4 as bundled with PHP 8.5.8
250000 placeholders: OK (0.33s)
250001 placeholders: SQLSTATE[HY000]: General error: 1 too many SQL variables
-- MySQL server 8.4.10
65535 placeholders: OK
65536 placeholders: SQLSTATE[HY000]: General error: 1390
Prepared statement contains too many placeholders
MySQL’s 65535 is a hard protocol limit — the placeholder count is a 16-bit field — so it is the same on every MySQL 8 server, tuning or not. SQLite’s is a compile-time constant (SQLITE_MAX_VARIABLE_NUMBER), so your SQLite may cut off somewhere else; the point is that a number exists and you will hit it long before your array feels large. PostgreSQL has its own 16-bit parameter limit. I did not measure Postgres or SQL Server for this page.
The SQLite and MySQL figures above, the empty-array SQL and the five-row delete can all be re-run with laravel-wherein-query-condition.sh, which shows a different SQLite cut-off as SKIP, runs its MySQL half only if a server answers as root, and leaves the scratch database it creates there for you to drop.
Long before the ceiling, the statement itself gets expensive to ship and parse. Measuring strlen($q->toSql()) at three sizes:
1000 placeholders -> SQL string 3,043 bytes
65535 placeholders -> SQL string 196,648 bytes
250000 placeholders -> SQL string 750,043 bytes
Two fixes, both of which keep the ID list inside the database:
// 1. Push the filter into a subquery — one binding instead of 250,000
Order::whereIn('customer_id', function ($q) {
$q->select('id')->from('customers')->where('country', 'NZ');
})->get();
// 2. Or chunk the array and accumulate
collect($ids)->chunk(1000)->each(function ($chunk) {
Order::whereIn('id', $chunk)->update(['flagged' => true]);
});
Where whereIn shows up without you writing it
Eager loading is built on it. Post::with('tags')->get() issues a second query whose where clause is an IN list of the parent IDs — which is also why eager loading a very large parent set can hit the same ceiling. The generated SQL and the query counts are in Laravel many-to-many relationships.
Finally: toSql() shows placeholders, never values. If you want the interpolated query for a bug report, log getBindings() alongside it rather than pasting values into the string — that habit is how whereIn stays injection-safe in the first place.