Laravel 8 插入

2021-07-19 11:14 更新

查詢構(gòu)造器還提供了 insert 方法用于插入記錄到數(shù)據(jù)庫(kù)中。 insert 方法接收數(shù)組形式的字段名和字段值進(jìn)行插入操作:

DB::table('users')->insert(
    ['email' => 'john@example.com', 'votes' => 0]
); 

你甚至可以將二維數(shù)組傳遞給 insert 方法,依次將多個(gè)記錄插入到表中:

DB::table('users')->insert([
    ['email' => 'taylor@example.com', 'votes' => 0],
    ['email' => 'dayle@example.com', 'votes' => 0],
]); 

insertOrIgnore 方法在將記錄插入數(shù)據(jù)庫(kù)時(shí)將忽略重復(fù)記錄錯(cuò)誤:

DB::table('users')->insertOrIgnore([
    ['id' => 1, 'email' => 'taylor@example.com'],
    ['id' => 2, 'email' => 'dayle@example.com'],
]); 
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)