updateOrInsert is one of those Laravel helpers that feels safe until it is not. You call it with a JSON column value, it works in local testing, and then it fails in production with Array to string conversion. The confusing part is that the same call works fine when the row already exists. It only fails when the row does not exist yet.
The symptom
Take a settings table with a JSON column:
DB::table('user_settings')->updateOrInsert(
['user_id' => $user->id],
['preferences' => ['theme' => 'dark', 'emails' => true]],
);
Run it once for a new user and the insert branch throws Array to string conversion. Run it again after the row exists and the update branch stores the JSON without complaint. The difference is which branch runs: the two branches handle array values differently.
What updateOrInsert actually does
The method is short, and reading it settles the question. From the query builder in Laravel 12:
public function updateOrInsert(array $attributes, array|callable $values = [])
{
$exists = $this->where($attributes)->exists();
if (! $exists) {
return $this->insert(array_merge($attributes, $values));
}
if (empty($values)) {
return true;
}
return (bool) $this->limit(1)->update($values);
}
There is no single upsert here. It is an existence check followed by either a plain insert() or a plain update(). Anything that differs between those two methods will show up as a bug that only fires on one side. Array values are exactly that kind of difference.
Why the update path works and the insert path does not
On the update path, each database grammar JSON-encodes array values before they are bound. The MySQL, PostgreSQL, and SQLite grammars all do this in their prepareBindingsForUpdate method, so an array meant for a JSON column arrives at PDO as a JSON string. On the insert path, that step was missing. The values were flattened and handed to PDO as raw bindings, and PHP tried to convert the array to a string when binding it. That is the Array to string conversion error.
So the bug has three conditions, and all three must hold: the value is an array, the row does not exist yet, and you are on an unpatched Laravel version. Remove any one of them and the call succeeds, which is why this bug survives code review and local testing. In local testing the row usually already exists from a seeder or an earlier run, so only the update branch ever executes.
The workaround that works today
Encode the array yourself before calling updateOrInsert:
DB::table('user_settings')->updateOrInsert(
['user_id' => $user->id],
['preferences' => json_encode(['theme' => 'dark', 'emails' => true])],
);
This works on every Laravel version, patched or not, because both branches receive a plain string. Only array values need this treatment. Strings, numbers, booleans, and nulls pass through unchanged. If you are on an older app where this pattern is used in several places, a small helper that walks the values array and JSON-encodes any array it finds keeps the fix in one place.
What the framework fix looks like
The proper fix is to make insert() treat array values the same way the update path already does. In Illuminate\Database\Query\Builder::insert(), JSON-encode array values before binding:
$bindings = (new Collection(Arr::flatten($values, 1)))
->map(fn ($value) => is_array($value) ? json_encode($value) : $value)
->all();
This is the fix for laravel/framework issue #62536. I reproduced the failure, applied this change, and verified it against the query builder test suite: 436 tests pass, and a new test confirms that inserting ['email' => 'foo', 'name' => ['bar', 'baz']] binds the JSON string '["bar","baz"]' instead of a raw array. The change is deliberately small. It mirrors what the grammars already do on update, so inserts and updates finally agree.
Where this bites in real code
Anywhere you store a JSON payload with an upsert-style call. User preferences and settings tables are the classic case. Sync jobs that write API responses into a JSON column are another. Calling it through a model does not help: Model::updateOrInsert forwards to the same query builder method, so Eloquent calls hit the same insert branch.
The order dependence is what makes it nasty. The first call for a given key takes the insert branch and fails. Every call after that takes the update branch and works. So the bug shows up exactly once per key, usually for a brand new user or record. If you see Array to string conversion in a queue job or a webhook handler that upserts settings, check whether the row existed before the call. That question alone will point you at this bug.
Takeaway
updateOrInsert is two methods wearing one name, and only one of them JSON-encoded array values. Until your Laravel version includes the fix, JSON-encode array values yourself before the call. And when a call works in testing but fails in production, check which branch actually ran. Read the source of the helper you are calling, it is usually shorter than you expect. For more on keeping database logic in the right layer, see Structuring a Laravel API: Controllers, Services, and Where Logic Lives.