Laravel查詢構造器的使用方法整理


1.結果集

1.1從一張表獲取所有行,get方法獲取所有行

$users = DB::table('users')->get();

獲取列的值

foreach ($users as $user) { 
echo $user->name;
}

1.2.從一張表中獲取一行/一列,first方法獲取單行

$user = DB::table('users')->where('name', 'John')->first();
echo $user->name;

1.3.如果想要獲取包含單個列值的數組,可以使用lists方法

$titles = DB::table('roles')->lists('title');
foreach ($titles as $title) {
echo $title;

1.4.聚合函數,比如count, max, min, avg, 和 sum,你可以在構造查詢之后調用這些方法

$users = DB::table('users')->count();
$price = DB::table('orders')->max('price');
$price = DB::table('orders') ->where('finalized', 1) ->avg('price')

1.5.我們並不總是想要獲取數據表的所有列,使用select方法查詢指定列

$users = DB::table('users')->select('name', 'email as user_email')->get();

1.6.distinct方法允許你強制查詢返回不重復的結果集

$users = DB::table('users')->distinct()->get();

1.7.內連接

$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();

1.8.左連接

$users = DB::table('users')
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->get();

2.where子句

調用where最基本的方法需要三個參數,第一個參數是列名,第二個參數是一個數據庫系統支持的任意操作符,第三個參數是該列要比較的值。

例如,下面是一個驗證“votes”列的值是否等於100的查詢:

$users = DB::table('users')->where('votes', '=', 100)->get();

為了方便,如果你只是簡單比較列值和給定數值是否相等,可以將數值直接作為where方法的第二個參數:

$users = DB::table('users')->where('votes', 100)->get();

其它操作符來編寫where子句:

$users = DB::table('users') 
->where('votes', '>=', 100)
->get();
$users = DB::table('users')
->where('votes', '<>', 100)
->get();
$users = DB::table('users')
->where('name', 'like', 'T%')
->get();

你可以通過方法鏈將多個where約束鏈接到一起,也可以添加or子句到查詢,orWhere方法和where方法接收參數一樣:

$users = DB::table('users') ->where('votes', '>', 100) ->orWhere('name', 'John') ->get();

whereBetween方法驗證列值是否在給定值之間(whereNotBetween方法驗證列值不在給定值之間):

$users = DB::table('users')
->whereBetween('votes', [1, 100])
->get();

whereIn方法驗證給定列的值是否在給定數組中(whereNotIn方法驗證給定列的值不在給定數組中):

$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();

whereNull方法驗證給定列的值為NULL(whereNotNull方法驗證給定列的值不是NULL):

$users = DB::table('users') 
->whereNull('updated_at')
->get();

3.orderBy方法

$users = DB::table('users') 
->orderBy('name', 'desc')
->get();

4.groupBy和having方法用於對結果集進行分組

$users = DB::table('users')
->groupBy('account_id')
->having('account_id', '>', 100)
->get();

ps:a.WHERE 子句用來篩選 FROM 子句中指定的操作所產生的行。
     b.GROUP BY 子句用來分組 WHERE 子句的輸出。
     c.HAVING 子句用來從分組的結果中篩選行。

5.skipskip/take,想要限定查詢返回的結果集的數目,或者在查詢中跳過給定數目的結果

$users = DB::table('users')->skip(10)->take(5)->get();

6.insert方法

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

如果數據表有自增ID,使用insertGetId方法來插入記錄將會返回ID值:

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

7.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']);

8.delete方法

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

在調用delete方法之前可以通過添加where子句對delete語句進行約束:

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

如果你希望清除整張表,也就是刪除所有列並將自增ID置為0,可以使用truncate方法:

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

 


免責聲明!

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



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