laravel eloquent ignore error when inserting a duplicate key

Solution:

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


Please note, insertOrIgnore will ignore duplicate records and also may ignore other types of errors depending on the database engine. For example, insertOrIgnore will bypass MySQL’s strict mode.