laravel 查詢構建器(連貫操作)


注:laravel 查詢返回的都是 php 的 stdClass 對象實例,不是數組!!!!

1)查詢多行(get)

DB::table('table_name')->get();

帶偏移和限制的查詢(skip take 或 offset limit)(兩種用法是一樣的)

//skip take
DB::table('table_name')->skip(10)->take(10)->get();

//offset limit
DB::table('table_name')->offset(20)->limit(10)->get();

 

2)查詢一行(first)

DB::table('table_name')->first();
DB::table('table_name')->find(1);

PS:find方法也可以查多行,它可以這樣玩 DB::table('table_name')->find([1,2,3,4]);

 

3)直接查詢一個字段(value)

DB::table('table_name')->where('name','Tiac')->value('email');

 

4)查詢一(pluck)

DB::table('table_name')->where('brand_id','100')->pluck('goods_id');

 

5)塊組結果集(chunk)

使用情景:

假設我們需要查詢 1 百萬的數據,並對其做處理,一次性查詢 1 百萬並統一處理勢必會對數據產生不小的壓力,消耗大量的服務器資源,有沒有一種更做優的處理方法?

將 1 百萬的查詢分成 1000 次 1000 條記錄的查詢的塊查詢怎樣?? 這個 chunk的作用,chunk方法接口一個閉包函數做回調處理

DB::table('users')->orderBy('id')->chunk(1000, function($users) {
    foreach ($users as $user) {
        //
    }
});

PS:上面只是舉例,chunk 貌似不能和 limit 一起用,chunk 默認情況下一次性處理整張表的數據,不過我可以通過自己加判斷用 return false 跳出 chunk 操作

 

6)聚合函數(count max min avg sum 等等)

DB::table('table_name')->count();

PS:其他聚合函數的使用方法一樣的,不一一舉例了

 

7)where 條件字句

這個比較多,懶得寫了,想看的直接到 laravel 學院看吧,傳送門:http://laravelacademy.org/post/6140.html#ipt_kb_toc_6140_8

(where, orWhere, whereNUll, whereIn, whereNotIn, whereBetween, whereNotBetween, whereDate, whereYear, whereMonth, whereDay 等等)

 

8)orderBy 排序字句

DB::table('table_name')->orderBy('id','desc')->get();

PS:如果想要返回隨機的查詢結果集,可以使用 inRandomOrder 子句

 

9)groupBy 分組字句及 having havingRaw 條件子句

DB::table('table_name')->select('goods_id')->groupBy('brand_id')->having('price','>','100')->get();
DB::table('table_name')->select('brand_id')->groupBy('brand_id')->havingRaw('sum(price)','>','10000');

 

10)使用原生sql表達式(DB::raw 方法)

舉個粟子:如果上面第9)點,我們想查詢出一個銷售額大於10000的品牌及銷售額,我們可以這樣子寫:

DB::table('table_name')->select('brand_id',DB::raw('sum(price) as sales_price'))->groupBy('brand_id')->having('sales_price','>','10000');

 

11)join 連接查詢子句

$users = DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();

更多用法,看這里:http://laravelacademy.org/post/6140.html#ipt_kb_toc_6140_6

 

12)insert 插入操作

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

插入多行記錄:

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

插入成功后返回自增id(使用insertGetId方法)

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

 

13)update 更新操作

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

增、減操作

DB::table('users')->increment('votes', 2);
DB::table('users')->decrement('votes', 1);

increment 和 decrement 方法還有update方法的功能,第三個參數直接寫要更新的數據的數組就可以了

DB::table('users')->increment('votes', 1, ['name' => 'Tiac']);

 

14)刪除、清空操作(delete truncate)

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

DB::table('users')->truncate();

 

15)使用游標(cursor)

有自己寫個框架的同學應該都知道,像 larvel 的 get 方法之類的操作,其實就是先查詢獲得一個查詢結果集(資源),再fetch資源,組裝返回一個數組,也就是說它已經先循環了一遍了,你拿到數據后,要處理的話往往又要循環一次數組;

如果查詢的數據較多,從效率、性能上考慮,我們可以省略這預先一次的循環(使用游標)

foreach (DB::table('table_name')->cursor() as $row) {
    //
}

 

大概這個樣子了,更詳情的說明,看這里:http://laravelacademy.org/post/6140.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM