Laravel 5:插入可能重复的行

时间:2015-11-09 13:58:30

标签: php mysql laravel laravel-5

如何在唯一列中插入可能重复的行,如果插入了项目则返回?

尝试:

$id = DB::insert("INSERT IGNORE INTO question (id,text) values (10,'test')");

$id始终返回值= 1

也试过了:

$id = DB::table('question')->insertGetId(['id' => 10, 'text' => 'test'])->ignore();

ignore()无效!

1 个答案:

答案 0 :(得分:0)

try {
    DB::table('arv_groups')->insert([
        'grouphash' => $grouphash,
        'id_photo' => $id
    ]);
} catch(\Illuminate\Database\QueryException $e){
    // I DON'T CARE IT'S DUPLICATE !!! THANK YOU VERY MUCH!
}