添加

2018-02-24 15:52 更新

添加數(shù)據(jù)進(jìn)數(shù)據(jù)表

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

添加自動遞增 (Auto-Incrementing) ID 的數(shù)據(jù)至數(shù)據(jù)表

如果數(shù)據(jù)表有自動遞增的ID,可以使用 insertGetId 添加數(shù)據(jù)并返回該 ID:

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

注意: 當(dāng)使用 PostgreSQL 時,insertGetId 方法會預(yù)期自動增加的字段是以「id」為命名。

添加多個數(shù)據(jù)進(jìn)數(shù)據(jù)表

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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號