《PHP實戰(zhàn):Laravel框架數(shù)據(jù)庫CURD操作、連貫操作總結(jié)》要點:
本文介紹了PHP實戰(zhàn):Laravel框架數(shù)據(jù)庫CURD操作、連貫操作總結(jié),希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP學習一、Selects
PHP學習檢索表中的所有行
代碼如下:
$users = DB::table('users')->get();
foreach ($users as $user)
{
var_dump($user->name);
}
PHP學習從表檢索單個行
代碼如下:
$user = DB::table('users')->where('name', 'John')->first();
var_dump($user->name);
檢索單個列的行
代碼如下:
$name = DB::table('users')->where('name', 'John')->pluck('name');
檢索一個列值列表
代碼如下:
$roles = DB::table('roles')->lists('title');
該辦法將返回一個數(shù)組標題的作用.你也可以指定一個自定義的鍵列返回的數(shù)組
代碼如下:
$roles = DB::table('roles')->lists('title', 'name');
指定一個Select子句
代碼如下:
$users = DB::table('users')->select('name', 'email')->get();
$users = DB::table('users')->distinct()->get();
$users = DB::table('users')->select('name as user_name')->get();
PHP學習Select子句添加到一個現(xiàn)有的查詢$query = DB::table('users')->select('name');
代碼如下:
$users = $query->addSelect('age')->get();
PHP學習where
代碼如下:
$users = DB::table('users')->where('votes', '>', 100)->get();
PHP學習OR
代碼如下:
$users = DB::table('users')->where('votes', '>', 100)->orWhere('name', 'John')->get();
PHP學習Where Between
代碼如下:
$users = DB::table('users')->whereBetween('votes', array(1, 100))->get();
PHP學習Where Not Between
代碼如下:
$users = DB::table('users')->whereNotBetween('votes', array(1, 100))->get();
PHP學習Where In With An Array
代碼如下:
$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();
$users = DB::table('users')->whereNotIn('id', array(1, 2, 3))->get();
PHP學習Using Where Null To Find Records With Unset Values
代碼如下:
$users = DB::table('users')->whereNull('updated_at')->get();
PHP學習Order By, Group By, And Having
代碼如下:
$users = DB::table('users')->orderBy('name', 'desc')->groupBy('count')->having('count', '>', 100)->get();
PHP學習Offset & Limit
代碼如下:
$users = DB::table('users')->skip(10)->take(5)->get();
PHP學習二、連接
PHP學習Joins
PHP學習查詢構(gòu)建器也可以用來編寫連接語句.看看下面的例子:
PHP學習Basic Join Statement
代碼如下:
DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.id', 'contacts.phone', 'orders.price')
->get();
PHP學習左連接語句
代碼如下:
DB::table('users')
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->get();
DB::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')->orOn(...);
})
->get();
DB::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);
})
->get();
PHP學習三、分組
PHP學習 有時候,您可能必要創(chuàng)建更高級的where子句,如“存在”或嵌套參數(shù)分組.Laravel query builder可以處理這些:
代碼如下:
DB::table('users')
->where('name', '=', 'John')
->orWhere(function($query)
{
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
上面的查詢將發(fā)生以下SQL:
代碼如下:
select * from users where name = 'John' or (votes > 100 and title
<> 'Admin')
Exists Statements
DB::table('users')
->whereExists(function($query)
{
$query->select(DB::raw(1))
->from('orders')
->whereRaw('orders.user_id = users.id');
})
->get();
PHP學習上面的查詢將發(fā)生以下SQL:
代碼如下:
select * from userswhere exists (
select 1 from orders where orders.user_id = users.id
)
PHP學習四、聚合
PHP學習查詢構(gòu)建器還提供了各種聚合辦法,如統(tǒng)計,馬克斯,min,avg和總和.
PHP學習Using Aggregate Methods
代碼如下:
$users = DB::table('users')->count();
$price = DB::table('orders')->max('price');
$price = DB::table('orders')->min('price');
$price = DB::table('orders')->avg('price');
$total = DB::table('users')->sum('votes');
PHP學習Raw Expressions
PHP學習有時您可能必要使用一個原始表達式的查詢.這些表達式將注入的查詢字符串,所以小心不要創(chuàng)建任何SQL注入點!創(chuàng)建一個原始表達式,可以使用DB:rawmethod:
PHP學習Using A Raw Expression
代碼如下:
$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();
PHP學習遞增或遞減一個列的值
代碼如下:
DB::table('users')->increment('votes');
DB::table('users')->increment('votes', 5);
DB::table('users')->decrement('votes');
DB::table('users')->decrement('votes', 5);
PHP學習您還可以指定額外的列更新:
代碼如下:
DB::table('users')->increment('votes', 1, array('name' => 'John'));
PHP學習Inserts
PHP學習將記錄插入表
代碼如下:
DB::table('users')->insert(
array('email' => 'john@example.com', 'votes' => 0)
);
PHP學習將記錄插入表自動增加的ID
PHP學習如果表,有一個自動遞增的id字段使用insertGetId插入一個記錄和檢索id:
代碼如下:
$id = DB::table('users')->insertGetId(
array('email' => 'john@example.com', 'votes' => 0)
);
PHP學習注意:當使用PostgreSQL insertGetId辦法預(yù)計,自增列被命名為“id”.
PHP學習多個記錄插入到表中
代碼如下:
DB::table('users')->insert(array(
array('email' => 'taylor@example.com', 'votes' => 0),
array('email' => 'dayle@example.com', 'votes' => 0),
));
PHP學習四、Updates
PHP學習更新一個表中的記錄
代碼如下:
DB::table('users')
->where('id', 1)
->update(array('votes' => 1));
PHP學習五、 Deletes
PHP學習刪除表中的記錄
代碼如下:
DB::table('users')->where('votes', '<', 100)->delete();
PHP學習刪除表中的所有記錄
代碼如下:
DB::table('users')->delete();
刪除一個表
代碼如下:
DB::table('users')->truncate();
PHP學習六、Unions
PHP學習查詢構(gòu)建器還提供了一種快速的辦法來“聯(lián)盟”兩個查詢:
代碼如下:
$first = DB::table('users')->whereNull('first_name');
$users =
DB::table('users')->whereNull('last_name')->union($first)->get();
PHP學習 unionAll辦法也可以,有相同的辦法簽名.
PHP學習 Pessimistic Locking
PHP學習 查詢構(gòu)建器包括一些“悲觀鎖定”功能來幫助你做你的SELECT語句. 運行SELECT語句“共享鎖”,你可以使用sharedLock辦法查詢:
代碼如下:
DB::table('users')->where('votes', '>',
100)->sharedLock()->get();
更新“鎖”在一個SELECT語句,您可以使用lockForUpdate辦法查詢:
代碼如下:
DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();
PHP學習七、緩存查詢
PHP學習 你可以輕松地緩存查詢的成果使用記憶法:
代碼如下:
$users = DB::table('users')->remember(10)->get();
在本例中,查詢的成果將為十分鐘被緩存.查詢成果緩存時,不會對數(shù)據(jù)庫運行,成果將從默認的緩存加載驅(qū)動程序指定您的應(yīng)用程序. 如果您使用的是支持緩存的司機,還可以添加標簽來緩存:
代碼如下:
$users = DB::table('users')->cacheTags(array('people', 'authors'))->remember(10)->get();
《PHP實戰(zhàn):Laravel框架數(shù)據(jù)庫CURD操作、連貫操作總結(jié)》是否對您有啟發(fā),歡迎查看更多與《PHP實戰(zhàn):Laravel框架數(shù)據(jù)庫CURD操作、連貫操作總結(jié)》相關(guān)教程,學精學透。維易PHP學院為您提供精彩教程。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/14759.html