INSERT IGNORE独特的电子邮件laravel 5.4

时间:2017-09-09 19:02:07

标签: php mysql arrays laravel

我正在使用maatwebsite将excel导入db和

$dataArray[] =
     [
         'name' => $row['name']
         'email' => $row['email'],
     ];

Apprentice::insert($dataArray);

打印变量时,结果为:

array:2 [▼
  0 => array:18 [▼
    "name" => "Maicol Stiven"
    "email" => "maic1ce@live.com"
  ]
  1 => array:18 [▼
    "name" => "Cristian Camilo"
    "email" => "carin45@gmail.com"
  ]
]

电子邮件是唯一的,我需要在电子邮件重复时,省略它并插入其他记录

我该怎么办?感谢

1 个答案:

答案 0 :(得分:0)

使用firstOrNew方法,如此

$a = Apprentice::firstOrNew(['email' => $row['email']]);
$a->name = $row['name'];
$a->save();