Laravel 資料庫操作
阿新 • • 發佈:2022-04-29
原生 SQL
插入 insert
使用 ? 繫結引數。
DB::insert('insert tb1 values(?,?,?)',[null,'tom',10]);
返回布林值
查詢 select
使用命名繫結引數。
DB::select('select * from tb1 where id = :id',['id'=>1]);
返回一個數組結果集,陣列中的每個結果將是一個 PHP
stdClass
物件。
更新 update
DB::update('update tb1 set name=? where id = ?',['khs1994',1]);
返回所影響的行數
刪除 delete
DB::delete('delete from tb1');
statement
DB::statement('drop table tb1');
資料庫請求構造器
獲取所有資料列 get
DB::table('tb1')->get();
返回一個
IlluminateSupportCollection
結果,其中每個結果都是一個 PHPStdClass
物件的例項
獲取單列或單行
獲取一行資料,使用 first
方法。
DB::table('tb1') ->where('name','John') ->first();
取出一行中的單個值。
DB::table('tb1')
->where('name','John')
->value('age')
獲取一列值。
DB::table('tb1')
->pluck('title')