MartinW12's avatar

How to do INSERT IGNORE

I have a database table in my database called "meetings" and a corresponding eloquent model called Meeting.

I am trying to do an INSERT IGNORE into the table.

I can do "Meeting::firstOrCreate()" but that is horribly inefficient, given I do not even want the model object itself.

However if I do "Meeting::createQuietly()" I get the following error:

Call to undefined method App\Models\Meeting::createQuietly()

Obviously I cannot do "Meeting::create()" because then I get an "Integrity constraint violation: 1062 Duplicate entry" error.

This is so basic that I cannot believe Laravel doesn't support it, so does anyone how I am supposed to do an INSERT IGNORE?

Just to clarify, I do not want "MyModel::firstOrCreate()" or "MyModel::createOrFirst()", I want to create the record if it doesn't exist and do nothing if it already exists, which translates to a SQL "INSERT IGNORE" statement.

1 like
10 replies
vincent15000's avatar

I can do "Meeting::firstOrCreate()" but that is horribly inefficient

Why ?

Yet it's the best choice for what you need.

If you really want to execute INSERT IGNORE, you can execute a raw query.

jlrdw's avatar

I want to create the record if it doesn't exist and do nothing if it already exists

Just do a regular search for the record.

1 like
MartinW12's avatar

To clarify , I do not want to read the record, I just want to insert it if it doesn't exist.

firstOrCreate() reads it which I do not want to do.

Is everyone saying that Laravel does not have a means to do "INSERT IGNORE" without doing a raw query or wrapping a "create()" inside a try...catch block and checking what type of exception was thrown?

1 like
jlrdw's avatar

@MartinW12 have you seen https://laravel.com/docs/12.x/queries#insert-statements

Edit:

As @snapey shows below that's why I referred you the the section of the documentation that covers this. From the documentation:

The insertOrIgnore method will ignore errors while inserting records into the database. When using this method, you should be aware that duplicate record errors will be ignored and other types of errors may also be ignored depending on the database engine. For example, insertOrIgnore will bypass MySQL's strict mode:

The example from the documentation:

DB::table('users')->insertOrIgnore([

    ['id' => 1, 'email' => '[email protected]'],

    ['id' => 2, 'email' => '[email protected]'],

]);
1 like
MartinW12's avatar

@jlrdw thanks, I was actually looking for insertOrIgnore at the model level, but I guess it doesn't have it, a suggestion for Laravel 13

1 like
Snapey's avatar

@MartinW12 How could it be applied at the model level?

Perhaps you just want to use eloquent instead of DB. Query builder methods can be applied to eloquent, so this should work

User::insertOrIgnore([

    ['id' => 1, 'email' => '[email protected]'],

    ['id' => 2, 'email' => '[email protected]'],

]);
2 likes
jlrdw's avatar

@MartinW12 It's referenced here:

Once you have created a model and its associated database table, you are ready to start retrieving data from your database. You can think of each Eloquent model as a powerful query builder allowing you to fluently query the database table associated with the model. The model's all method will retrieve all of the records from the model's associated database table:

https://laravel.com/docs/12.x/eloquent#retrieving-models

Where there is a direct link to query builder. So yes eloquent has all the query builder methods included.

1 like
MartinW12's avatar

Derr!

I originally had "createQuietly()" and I thought I had tried "insertOrIgnore()" but obviously I hadn't :-(

Thanks @Snapey

1 like
Snapey's avatar

@MartinW12 the quietly methods are for interacting with models without triggering any observers (model events)

Please select best answer if you are solved

1 like

Please or to participate in this conversation.