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

Add INSERT IGNORE support to Query Builder #9612

Closed
gephaest opened this issue Jul 13, 2015 · 15 comments
Closed

Add INSERT IGNORE support to Query Builder #9612

gephaest opened this issue Jul 13, 2015 · 15 comments

Comments

@gephaest
Copy link

It would be very useful to have ability to use IGNORE statement like this
DB::connection('foo')->table('bar')->insert($rows)->ignore();

For additional details check MySQL docs: http://dev.mysql.com/doc/refman/5.6/en/insert.html

@ArjanSchouten
Copy link
Contributor

Yes but is it supported by mssql, postgresql and sqlite?

@dtaub
Copy link
Contributor

dtaub commented Jul 13, 2015

A quick google shows that SQLite supports a somewhat similar construct but for the other DBMS's you would need to either use WHERE NOT EXISTS or some form of exception catching. WHERE NOT EXISTS would probably be the most cross-compatible way to go about it.

@gephaest
Copy link
Author

ignore() can be alias to WHERE NOT EXISTS. Ignore looks clear and understandable.

@dtaub
Copy link
Contributor

dtaub commented Jul 15, 2015

Also, you can't chain onto insert() since that method actually runs the statement, so it would have to be something more like:

DB::connection('foo')->table('bar')->insertIgnore($rows);

@GrahamCampbell
Copy link
Member

You probably just want to use a raw query here.

@jfoliveira
Copy link

Using raw query is fine for inserting a single row/array.

However, as the insert function supports an array of multiple rows to be inserted at a time - insert($multipleRows) - it would be very convenient to have INSERT IGNORE INTO implemented in the QueryBulider.

@jfoliveira
Copy link

A good soul has published a package that can add this capability to a model: https://github.com/jdavidbakr/replaceable-model

@jbruni
Copy link
Contributor

jbruni commented Jul 17, 2017

Here is what worked for me: https://stackoverflow.com/a/45151709/370290

@phpguru
Copy link
Contributor

phpguru commented May 17, 2018

Because this very old issue is the # 1 hit on google for insert ignore query builder... what works best for us is firstOrCreate.

@mtrajano
Copy link

firstOrCreate is great if you only need to insert one row, but like someone else said the great thing about insert is that it lets you pass multiple rows at once, I still think a insertIgnore would be a good addition

@gpressutto5
Copy link

@phpguru firstOrCreate is for Eloquent. Query builder isn't the same as Eloquent.

@dunhamjared
Copy link
Contributor

@GrahamCampbell Could we re-open this? Is there anyway we could vote on this feature? Judging from the responses on this issue, this feature would be very useful for many people who use databases that allow insert ignore.

@dunhamjared
Copy link
Contributor

Found this great Laravel extension that adds support for INSERT & UPDATE (UPSERT) and INSERT IGNORE to the query builder and Eloquent: staudenmeir/laravel-upsert

@dunhamjared
Copy link
Contributor

dunhamjared commented Sep 11, 2019

Laravel's Query Builder now has insertOrIgnore in version v5.8.33 and up:

<?php
DB::table('users')->insertOrIgnore([
    ['id' => 1, 'email' => 'taylor@example.com'],
    ['id' => 2, 'email' => 'dayle@example.com']
]);

Read more here: https://laravel.com/docs/5.8/queries#inserts

@iwasherefirst2
Copy link

@dunhamjared thank you! Exactly what I was looking for.

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

No branches or pull requests