INSERT IGNORE using Laravel's Fluent

前端 未结 10 1322
执笔经年
执笔经年 2020-12-11 02:12

Is there a quick way to modify a SQL query generated by Laravel\'s Fluent to have an INSERT IGNORE instead of the usual INSERT?

I\'m trying

10条回答
  •  情书的邮戳
    2020-12-11 02:55

    Answer for Laravel 5.8.33+

    If anyone reads this nowadays: there's no need for any hacks or Query Builder extensions. The query builder natively provides an insertOrIgnore method that does just that.

    Just use

    DB::table('tablename')->insertOrIgnore([
        ['column_name' => 'row1', 'column2_name' => 'row1'],
        ['column_name' => 'row2', 'column2_name' => 'row2']
    ]);
    

    See the documentation or the API docs for details.

提交回复
热议问题