執(zhí)行查找

2018-02-24 15:52 更新

如果配置好數(shù)據(jù)庫(kù)連接,就可以通過(guò) DB facade 執(zhí)行查找。

執(zhí)行 Select 查找

$results = DB::select('select * from users where id = ?', [1]);

select 方法會(huì)返回一個(gè) array 結(jié)果。

You may also execute a query using named bindings:

$results = DB::select('select * from users where id = :id', ['id' => 1]);

執(zhí)行 Insert 語(yǔ)法

DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle']);

執(zhí)行 Update 語(yǔ)法

DB::update('update users set votes = 100 where name = ?', ['John']);

執(zhí)行 Delete 語(yǔ)法

DB::delete('delete from users');

注意: update 和 delete 語(yǔ)法會(huì)返回在操作中所影響的數(shù)據(jù)筆數(shù)。

執(zhí)行一般語(yǔ)法

DB::statement('drop table users');

監(jiān)聽(tīng)查找事件

你可以使用 DB::listen 方法,去監(jiān)聽(tīng)查找的事件:

DB::listen(function($sql, $bindings, $time)
{
    //
});
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)