Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add an Upsert method #8

Open
JasonTheAdams opened this issue Oct 25, 2022 · 0 comments
Open

Feature: Add an Upsert method #8

JasonTheAdams opened this issue Oct 25, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@JasonTheAdams
Copy link

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".

@JasonTheAdams JasonTheAdams added the enhancement New feature or request label Oct 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant