首頁 php教程 PHP开发 Laravel框架資料庫CURD操作、連貫操作總結

Laravel框架資料庫CURD操作、連貫操作總結

Dec 27, 2016 am 10:21 AM

一、Selects

檢索表中的所有行

$users = DB::table('users')->get();
foreach ($users as $user)
{
var_dump($user->name);
}
登入後複製

從表檢索單行

$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');
登入後複製

該方法將傳回一個數組標題的作用。你也可以指定一個自訂的鍵列傳回的陣列

$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();
登入後複製

Select子句加入到一個現有的查詢$query = DB::table('users')->select( 'name');

$users = $query->addSelect('age')->get();
登入後複製

where

$users = DB::table('users')->where('votes', '>', 100)->get();
登入後複製

OR

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

Where Between

$users = DB::table('users')->whereBetween('votes', array(1, 100))->get();
登入後複製

Where Not Between

$users = DB::table('users')->whereNotBetween('votes', array(1, 100))->get();
登入後複製

語 Where An Ar Arals 這些der By, Group By, And Having

$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();
$users = DB::table('users')->whereNotIn('id', array(1, 2, 3))->get();
登入後複製

Offset & Limit

$users = DB::table('users')->whereNull('updated_at')->get();
登入後複製

二、連接

Joins

查詢建構器也可以用來寫連接語句。看看下面的範例:

Basic Join Statement

$users = DB::table('users')->orderBy('name', 'desc')->groupBy('count')->having('count', '>', 100)->get();
登入後複製

左連接語句

$users = DB::table('users')->skip(10)->take(5)->get();
登入後複製

三、分組

  有時候,您可能需要建立更高級的where子句,「存在」或嵌套或巢狀參數分組參數。 Laravel query builder可以處理這些:

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();
登入後複製

  上面的查詢將產生以下SQL:

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();
登入後複製

上面的查詢將產生以下SQL:

DB::table('users')
->where('name', '=', 'John')
->orWhere(function($query)
{
$query->where('votes', '>', 100)
->where(&#39;title&#39;, &#39;<>&#39;, &#39;Admin&#39;);
})
->get();
登入後複製

四、聚合

構建器還提供了各種查詢方法如統計,馬克斯,min,avg和總和。

Using Aggregate Methods

  select * from users where name = &#39;John&#39; or (votes > 100 and title 
<> &#39;Admin&#39;)
  Exists Statements
  DB::table(&#39;users&#39;)
  ->whereExists(function($query)
  {
  $query->select(DB::raw(1))
  ->from(&#39;orders&#39;)
  ->whereRaw(&#39;orders.user_id = users.id&#39;);
  })
  ->get();
登入後複製

Raw Expressions

有時您可能需要使用一個原始表達式的查詢。這些表達式將注入的查詢字串,所以小心不要創建任何SQL注入點!創建一個原始表達式,可以使用DB:rawmethod:

Using A Raw Expression

select * from userswhere exists (
select 1 from orders where orders.user_id = users.id
)
登入後複製

遞增或遞減一個列的值

$users = DB::table(&#39;users&#39;)->count();
$price = DB::table(&#39;orders&#39;)->max(&#39;price&#39;);
$price = DB::table(&#39;orders&#39;)->min(&#39;price&#39;);
$price = DB::table(&#39;orders&#39;)->avg(&#39;price&#39;);
$total = DB::table(&#39;users&#39;)->sum(&#39;votes&#39;);
登入後複製

您也可以指定額外的列更新:

$users = DB::table(&#39;users&#39;)
->select(DB::raw(&#39;count(*) as user_count, status&#39;))
->where(&#39;status&#39;, &#39;<>&#39;, 1)
->groupBy(&#39;status&#39;)
->get();
登入後複製

Inserts

將記錄插入表

DB::table(&#39;users&#39;)->increment(&#39;votes&#39;);
DB::table(&#39;users&#39;)->increment(&#39;votes&#39;, 5);
DB::table(&#39;users&#39;)->decrement(&#39;votes&#39;);
DB::table(&#39;users&#39;)->decrement(&#39;votes&#39;, 5);
登入後複製

將記錄插入表自動增加的ID

如果表,有一個自動遞增的id字段使用insertGetId插入一個記錄和檢索:

 DB::table(&#39;users&#39;)->increment(&#39;votes&#39;, 1, array(&#39;name&#39; => &#39;John&#39;));
登入後複製

注意:當使用PostgreSQL insertGetId方法預計,自增列被命名為「id」。

多個記錄插入到表中

DB::table(&#39;users&#39;)->insert(
array(&#39;email&#39; => &#39;john@example.com&#39;, &#39;votes&#39; => 0)
);
登入後複製

四、Updates

更新一個表中的記錄

$id = DB::table(&#39;users&#39;)->insertGetId(
array(&#39;email&#39; => &#39;john@example.com&#39;, &#39;votes&#39; => 0)
);
登入後複製

五、 Deletes

刪除表中的記錄

五、 Deletes

刪除表中的記錄刪除表中的記錄刪除表中的記錄表中的所有記錄。表

DB::table(&#39;users&#39;)->insert(array(
array(&#39;email&#39; => &#39;taylor@example.com&#39;, &#39;votes&#39; => 0),
array(&#39;email&#39; => &#39;dayle@example.com&#39;, &#39;votes&#39; => 0),
));
登入後複製

六、Unions

查詢建構器也提供了一種快速的方法來「聯盟」兩個查詢:

DB::table(&#39;users&#39;)
->where(&#39;id&#39;, 1)
->update(array(&#39;votes&#39; => 1));
登入後複製

  unionAll方法也可以,有相同的方法簽署。

  Pessimistic Locking

  查詢建構器包含一些「悲觀鎖定」功能來幫助你做你的SELECT語句。執行SELECT語句「共享鎖定」,你可以使用sharedLock方法查詢:

DB::table(&#39;users&#39;)->where(&#39;votes&#39;, &#39;<&#39;, 100)->delete();
登入後複製

更新「鎖」在一個SELECT語句,您可以使用lockForUpdate方法查詢:

DB::table(&#39;users&#39;)->delete();
登入後複製

七、快取查詢

🀜 㟎地你可以輕鬆地快取的快取的結果使用記憶法:

DB::table(&#39;users&#39;)->truncate();
登入後複製

  在本例中,查詢的結果將為十分鐘被緩存。查詢結果快取時,不會對資料庫執行,結果將從預設的快取載入驅動程式指定您的應用程式。  如果您使用的是支援快取的司機,還可以添加標籤來快取:

  $first = DB::table(&#39;users&#39;)->whereNull(&#39;first_name&#39;);
  $users = 
DB::table(&#39;users&#39;)->whereNull(&#39;last_name&#39;)->union($first)->get();
登入後複製

更多Laravel框架資料庫CURD操作、連貫操作總結相關文章請關注PHP中文網!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1665
14
CakePHP 教程
1423
52
Laravel 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24