Upsert rows without a per-row form

upsert writes a batch by a unique key. It skips model events, so a normal Vue form should still use create or update.

For your AI agent

Copies a prompt. Paste it into the AI agent you already have open.

Read https://laravel-vuejs.space/laravel-upsert-vue.md
upsert writes a batch by a unique key. It skips model events, so a normal Vue form should still use create or update.
Do not point an Inertia form at upsert for one title.
Do not upsert from Vue by posting the whole table on every keystroke.
Show the diff. Do not commit.
On this page · 3 sections
  1. The fix
  2. What to avoid
  3. FAQ

People searching “Laravel upsert” use it for the post form so they can skip the id. A form that creates or edits one post should use create or update from the CRUD guide. upsert is a batch import.

TL;DR

  • upsert writes a batch by a unique key.
  • It skips model events, so a normal Vue form should still use create or update.
  • Do not point an Inertia form at upsert for one title.
  • Do not upsert from Vue by posting the whole table on every keystroke.

Who it's for: Laravel + Vue developers

Applies to: Laravel 13 · Vue 3

The fix#

php
Post::upsert([
    ['slug' => 'hello', 'title' => 'Hello', 'user_id' => $request->user()->id],
], uniqueBy: ['slug'], update: ['title']);

uniqueBy is the unique index. update is the column list that changes when the slug already exists. Timestamps are not filled unless you pass them in the row. Observers and model events do not run.

Validate each row before this call. upsert is not a validator.

What to avoid#

Gotcha

Do not point an Inertia form at upsert for one title. You lose the policy events and the id comes back differently. Do not upsert from Vue by posting the whole table on every keystroke.

FAQ#

How do I upsert rows without a per-row form?

upsert writes a batch by a unique key. It skips model events, so a normal Vue form should still use create or update.

What goes wrong with Laravel upsert?

Many developers use it for the post form so they can skip the id. Do not point an Inertia form at upsert for one title.

Found this useful?

Ask questions, share what you built.

Join thousands of Laravel and Vue developers in our Facebook group, or get new posts in your Telegram feed. Sharing this article helps too.

Keep reading

Newsletter

Get the best of Laravel & Vue, once a month.

One short email with new guides, release notes worth reading and hand-picked community links. No spam, unsubscribe any time.