Skip to content

Feature: Add an Upsert method #8

Description

@JasonTheAdams

Often times it's useful to update existing rows if present and insert if missing. An example of this in WordPress is the update_post_meta function. It would be useful to be able to do something like this:

DB::table('flights')->upsert(
    [
        ['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99],
        ['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150]
    ],
    ['departure', 'destination'],
    ['price']
);

Arguments:

  1. The records to insert or update
  2. The column(s) that uniquely identify whether the record already exists
  3. The column(s) to update if the record does exist

Under the hood this will probably use the ON DUPLICATE KEY UPDATE SQL syntax, as such it will be important that the identifier column(s) are either "primary" or "unique".

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions