執(zhí)行查找

2018-02-24 15:52 更新

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

執(zhí)行 Select 查找

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

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

You may also execute a query using named bindings:

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

執(zhí)行 Insert 語法

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

執(zhí)行 Update 語法

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

執(zhí)行 Delete 語法

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

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

執(zhí)行一般語法

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

監(jiān)聽查找事件

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

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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號