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

[8.x] Support created_at timestamps on upserts #34712

Merged
merged 7 commits into from Oct 7, 2020
Merged

[8.x] Support created_at timestamps on upserts #34712

merged 7 commits into from Oct 7, 2020

Conversation

paras-malhotra
Copy link
Contributor

As a follow-up to #34698, this PR adds support for updating created_at timestamps on upserts.

I've modified the signature of the upsert method to include an optional third argument to be an array of the columns to be updated. If not provided, all columns will be updated.

@taylorotwell
Copy link
Member

Should we update DatabaseQueryBuilderTest to check the the raw SQL is correct using the new feature?

@paras-malhotra
Copy link
Contributor Author

@taylorotwell, I've added DatabaseQueryBuilderTest tests for the new feature as well.

@taylorotwell
Copy link
Member

I still don't really understand this method. Assume I have a Post database model. The table has slug (unique), title (string), and creation / update timestamps.

I want to bulk edit my post titles and upsert the posts... any new records should be inserted with a created at timestamp. I can't do this:

    $results = Post::upsert([
        ['slug' => 'foo', 'title' => 'foo updated'],
        ['slug' => 'bar', 'title' => 'bar updated'],
    ], 'slug');

What am I supposed to do?

@paras-malhotra
Copy link
Contributor Author

@taylorotwell for that case, you'd have to do this:

$results = Post::upsert([
        ['slug' => 'foo', 'title' => 'foo updated'],
        ['slug' => 'bar', 'title' => 'bar updated'],
], 'slug', ['title']);

@taylorotwell taylorotwell merged commit 338ffa6 into laravel:8.x Oct 7, 2020
@paras-malhotra paras-malhotra deleted the bulk_upsert branch October 7, 2020 16:51
LukeTowers added a commit to octobercms/library that referenced this pull request Oct 19, 2020
bennothommo added a commit to octobercms/library that referenced this pull request Oct 20, 2020
LukeTowers added a commit to octobercms/library that referenced this pull request Oct 20, 2020
Pulls in the work by @paras-malhotra in https:ithub.com/laravel/framework/pull/34698 & laravel/framework#34712 for use in October CMS.

Co-authored-by: Ben Thomson <git@alfreido.com>
@mariomka
Copy link

mariomka commented Nov 26, 2020

Amazing work, I love it!

This PR also adds the possibility to define the value when a row is being updated, and it allows DB expressions. It is awesome, for example, you can add numbers:

Stats::upsert(
    [
        ['id' => 1, 'page_views' => 1],
        ['id' => 2, 'page_views' => 5],
    ],
    'id',
    [
        'page_views' => DB::raw('stats.page_views + excluded.page_views'),
    ]
);

It works for Postgres

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants