laravel 查詢構(gòu)造器


1、獲取所有值
DB::table('users')->get()

2、獲取一條數(shù)據(jù)
DB::table('users')->where('name','alex')->first();

3、獲取某個字段
DB::table('users')->where('name','alex')->value('email');

4、獲取一列值
DB::table('users')->plunk('name');
你也可以在返回的數(shù)組中指定自定義的鍵值字段:
DB::table('users')->plunk('name','age');
返回值會是:[ 'age1'=>'name1','age2'=>'name2'... ]

5、結(jié)果分塊
chunk(),先不看

6、聚合
DB::table('users')->count();
DB::table('orders')->max('price');
DB::table('orders')->where('finalized', 1)->avg('price');

7、selects
指定返回字段
DB::table('users')->select('name', 'email as user_email')->get();
distinct 返回不重復(fù)結(jié)果
DB::table('users')->distinct()->get();
如果你已有一個查詢構(gòu)造器實例,并且希望在現(xiàn)有的 select 子句中加入一個字段,則可以使用 addSelect 方法:
$query = DB::table('users')->select('name');
$users = $query->addSelect('age')->get();

8、原始表達(dá)式
$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();

9、Joins
不會,后期補(bǔ)充

10、Unions(合并)
$first = DB::table('users')
->whereNull('first_name');

$users = DB::table('users')
->whereNull('last_name')
->union($first)
->get();

11、where

where('votes', '>=', 100) where('name', 'like', 'T%')
或數(shù)組
where([
['status', '=', '1'],
['subscribed', '<>', '1'],
])

其他還有orWhere,whereBetween,whereNotBetween,WhereIn,whereNotIn,whereNull 與 whereNotNull,whereDate / whereMonth / whereDay / whereYear,

whereColumn方法用來檢測兩個列的數(shù)據(jù)是否一致
whereColumn('first_name', 'last_name')
還可以使用運算符
whereColumn('updated_at', '>', 'created_at')
可以接收數(shù)組參數(shù)
whereColumn([
['first_name', '=', 'last_name'],
['updated_at', '>', 'created_at']
])
where還有很多用法,用到時可以詳細(xì)研究

12、Ordering, Grouping, Limit 及 Offset
用到時查詢

13、條件語句
when() 之后再補(bǔ)充

增 insert

DB::table('users')->insert(
['email' => 'john@example.com', 'votes' => 0]
);
若數(shù)據(jù)表存在自增 id,則可以使用 insertGetId 方法來插入記錄并獲取其 ID:
$id = DB::table('users')->insertGetId(
['email' => 'john@example.com', 'votes' => 0]
);

改 update

DB::table('users')
->where('id', 1)
->update(['votes' => 1]);

自增或自減
DB::table('users')->increment('votes');
DB::table('users')->increment('votes', 5);
DB::table('users')->decrement('votes');
DB::table('users')->decrement('votes', 5);

您還可以指定要操作中更新其它字段:
DB::table('users')->increment('votes', 1, ['name' => 'John']);

刪 delete

DB::table('users')->delete();
DB::table('users')->where('votes', '>', 100)->delete();

如果你需要清空表,你可以使用 truncate 方法,這將刪除所有行,并重置自動遞增 ID 為零:
DB::table('users')->truncate();

eloquent 模型更好用應(yīng)該,還沒看到,后期補(bǔ)充

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • laravel數(shù)據(jù)庫使用簡易說明 首先可以使用查詢構(gòu)造器和EloquentORM兩種方式 目前支持的數(shù)據(jù)庫類型有:...
    hankviv閱讀 804評論 0 0
  • 轉(zhuǎn)載,覺得這篇寫 SQLAlchemy Core,寫得非常不錯。不過后續(xù)他沒寫SQLAlchemy ORM... ...
    非夢nj閱讀 5,607評論 1 14
  • 配置 修改config/database.php在connection數(shù)組中添加mongodb的配置信息,如下 '...
    jooohnny閱讀 8,620評論 3 8
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,711評論 19 139
  • MySQL 數(shù)據(jù)庫常用命令 1、MySQL常用命令 create database name; 創(chuàng)建數(shù)據(jù)庫 use...
    55lover閱讀 5,065評論 1 57

友情鏈接更多精彩內(nèi)容