2017-09-09 57 views
0

我使用maatwebsite導入Excel到db和INSERT忽略唯一的電子郵件laravel 5.4

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

Apprentice::insert($dataArray); 

當變量打印的結果是:

array:2 [▼ 
    0 => array:18 [▼ 
    "name" => "Maicol Stiven" 
    "email" => "[email protected]" 
    ] 
    1 => array:18 [▼ 
    "name" => "Cristian Camilo" 
    "email" => "[email protected]" 
    ] 
] 

電子郵件是一種獨特的,我需要在電子郵件重複時省略並插入其他記錄

我該怎麼辦?感謝

回答

0

使用firstOrNew方法,像這樣

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