Laravel bulk insert fails with 'too many placeholders'
SQLSTATE[HY000]: General error: 1390 Prepared statement contains too many placeholdersShort answer
Laravel sends one placeholder per value, so one insert of many rows times many columns, or a whereIn with a huge list, creates a statement MySQL refuses. Split the work with collect($rows)->chunk(500) and run one insert per chunk. Use upsert() if rows may already exist.
Fix it fast
1Find out which cause you have
Work out how many placeholders your query makes: rows times columns for an insert, or the length of the list for whereIn. Log that number before the query.
$rows = count($data) * count($data[0]); logger()->info("placeholders: {$rows}");2Match what you see, then jump to its fix
- 1
The failing call is DB::table(...)->insert($bigArray) or Model::insert($bigArray).One insert() call carries every rowFix - 2
The failing query is a select, update or delete with whereIn('id', $ids).A whereIn list with thousands of idsFix - 3
After you chunk the insert, a re-run stops on duplicate keys.Rows may already exist, so you re-run a failed importFix
- 1
What does the error look like?
SQLSTATE[HY000]: General error: 1390 Prepared statement contains too many placeholders
Cause 1 of 3
One insert() call carries every row
1Check
Laravel accepts an array of arrays and builds a single statement from it. MySQL's prepare step sends the number of parameters in a 2-byte field, so a statement with tens of thousands of values is refused.
logger()->info('rows: '.count($rows).', columns: '.count($rows[0]));2Fix
Break the array into chunks and insert each one. 500 rows per chunk is a safe start for a table with a few dozen columns.
use Illuminate\Support\Facades\DB;
collect($rows)->chunk(500)->each(function ($chunk) {
DB::table('products')->insert($chunk->toArray());
});3Verify
Run the import again and count the rows.
php artisan tinker --execute="echo DB::table('products')->count();"Confirmed by laravel.com
Cause 2 of 3
A whereIn list with thousands of ids
1Check
Each value in a whereIn list becomes its own placeholder.
logger()->info('ids: '.count($ids));2Fix
Process the ids in slices, or fetch the rows with a subquery or join instead of a pasted list.
collect($ids)->chunk(1000)->each(function ($slice) {
DB::table('orders')->whereIn('id', $slice->all())->update(['status' => 'done']);
});3Verify
The update finishes with no HY000 error.
php artisan tinker --execute="echo DB::table('orders')->where('status','done')->count();"Confirmed by laravel.com
Cause 3 of 3
Rows may already exist, so you re-run a failed import
1Check
A first run that failed halfway has already written some chunks.
php artisan tinker --execute="echo DB::table('products')->count();"2Fix
Use upsert(), which inserts new rows and updates existing ones in one statement per chunk. Its second argument names the columns that identify a row.
collect($rows)->chunk(500)->each(function ($chunk) {
DB::table('products')->upsert($chunk->toArray(), ['sku'], ['price', 'name']);
});3Verify
Run the import twice; the row count does not grow the second time.
php artisan tinker --execute="echo DB::table('products')->count();"Confirmed by laravel.com
How do I stop it coming back?
- Never pass a whole file to insert(); chunk first.
- Wrap the chunk loop in DB::transaction() if a half-finished import is not acceptable.
- Use a queued job per chunk for imports that run longer than a web request.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
- EZ EstimaterConstruction quoting and job tracking for contractors, on a Laravel and Firebase stack.
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
Related fixes
Databases
Laravel query fails: conversion from one collation into another is impossible
SQLSTATE[HY000]: General error: 3988 Conversion from collation utf8_unicode_ci into utf8mb4_0900_ai_ci impossible for parameter
MySQL is comparing or binding a value whose character set cannot be converted into the one on the other side.
3 causes, each with a fixDatabases
Laravel migration fails on managed MySQL because a table has no primary key
Add a primary key to the table or unset this variable to avoid this message.
The server has sql_require_primary_key switched on, so every CREATE TABLE or ALTER TABLE must leave the table with a primary key.
3 causes, each with a fixDatabases
PHP cannot connect to MySQL: the connection attempt failed
Error 2003
Error 2003 means the TCP connection to MySQL never opened.
4 causes, each with a fixDatabases
The client cannot reach the MySQL or MariaDB server through its Unix socket file
ERROR 2002 (HY000): Can't connect to local server through socket '/run/mysqld/mysqld.sock' (2) (MariaDB client wording)
Error 2002 means the client tried the Unix socket file named in the message and found nothing it could talk to.
4 causes, each with a fixDatabases
MySQL or MariaDB: Column count of mysql.proc is wrong, table probably corrupted
Error 1558: Column count of mysql.proc is wrong
The mysql.proc system table has a different number of columns than the running server expects.
3 causes, each with a fixDatabases
Saving an empty date from PHP into a MySQL DATETIME column fails
The column holds 0000-00-00 00:00:00 instead of NULL
An empty string is not NULL.
4 causes, each with a fix