欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

怎么在Laravel5.1中使用查詢構(gòu)建器-創(chuàng)新互聯(lián)

本篇文章為大家展示了怎么在Laravel5.1 中使用查詢構(gòu)建器,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

在吉安等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都做網(wǎng)站、成都網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需網(wǎng)站開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計(jì),營銷型網(wǎng)站,外貿(mào)營銷網(wǎng)站建設(shè),吉安網(wǎng)站建設(shè)費(fèi)用合理。

1 查詢結(jié)果

先來看看它的語法:

  public function getSelect()
  {
    $result = DB::table('articles')->get();
    dd($result);
  }

查詢構(gòu)建器就是通過table方法返回的,使用get()可以返回一個結(jié)果集(array類型) 這里是返回所有的數(shù)據(jù),當(dāng)然你也可以鏈接很多約束。

1.1 獲取一列/一行數(shù)據(jù)

  public function getSelect()
  {
    $result = DB::table('articles')->where('title', 'learn database')->get();  // 獲取整列數(shù)據(jù)
    $articles = DB::table('articles')->where('title', 'learn database')->first(); // 獲取一行數(shù)據(jù)
    dd($result, $articles);
  }

我們可以通過where來增添?xiàng)l件。

1.2 獲取數(shù)據(jù)列值列表

如果你想要取到某列的值的話 可以使用lists方法:

  public function getSelect()
  {
    $result = DB::table('articles')->where('id', '<', 2)->lists('title');
    $titles = DB::table('articles')->lists('title');
    dd($result, $titles);
  }

1.3 獲取組塊兒結(jié)果集

在我們數(shù)據(jù)表中數(shù)據(jù)特別特別多時(shí) 可以使用組塊結(jié)果集 就是一次獲取一小塊數(shù)據(jù)進(jìn)行處理

  public function getSelect()
  {
    DB::table('articles')->chunk(2, function ($articles){
      foreach ($articles as $article){
        echo $article->title;
        echo "<br />";
      }
    });
  }

如果說要終止組塊運(yùn)行的話 返回false就可以了:

  public function getSelect()
  {
    DB::table('articles')->chunk(2, function ($articles){
      return false;
    });
  }

1.4 聚合函數(shù)

構(gòu)建器還提供了很多的實(shí)用方法供我們使用:

  • count方法:返回構(gòu)建器查詢到的數(shù)據(jù)量。

  • max方法:傳入一列 返回這一列中較大的值。

  • min方法:跟max方法類似,它返回最小的值。

  • sum方法:返回一列值相加的和。

  • avg方法:計(jì)算平均值。

1.4.1 count
  public function getArticlesInfo()
  {
    $article_count = DB::table('articles')->count();
    dd($article_count);
  }
1.4.2 max
  public function getArticlesInfo()
  {
    $maxCommentCount = DB::table('articles')->max('comment_count');
    dd($maxCommentCount);
  }
1.4.3 sum
  public function getArticlesInfo()
  {
    $commentSum = DB::table('articles')->sum('comment_count');
  }
1.4.4 avg
  public function getArticlesInfo()
  {
    $commentAvg = DB::table('articles')->avg('comment_count');
    dd($commentAvg);
  }

1.5 select查詢

1.5.1 自定義子句

select語句可以獲取指定的列,并且可以自定義鍵:

  public function getArticlesInfo()
  {
    $articles = DB::table('articles')->select('title')->get();
    // 輸出結(jié)果:
//    array:3 [▼
//      0 => {#150 ▼
//          +"title": "laravel database"
//      }
//      1 => {#151 ▼
//          +"title": "learn database"
//       }
//       2 => {#152 ▼
//          +"title": "alex"
//       }
//      ]
    $articles1 = DB::table('articles')->select('title as articles_title')->get();
    // 輸出結(jié)果:
//    array:3 [▼
//       0 => {#153 ▼
//          +"articles_title": "laravel database"
//       }
//       1 => {#154 ▼
//          +"articles_title": "learn database"
//       }
//       2 => {#155 ▼
//          +"articles_title": "alex"
//       }
//      ]
    $articles2 = DB::table('articles')->select('title as articles_title', 'id as articles_id')->get();
//    array:3 [▼
//       0 => {#156 ▼
//          +"articles_title": "laravel database"
//          +"articles_id": 1
//       }
//       1 => {#157 ▼
//          +"articles_title": "learn database"
//          +"articles_id": 2
//       }
//       2 => {#158 ▼
//          +"articles_title": "alex"
//          +"articles_id": 3
//       }
//      ]
  }
1.5.2 distinct方法

關(guān)于distinct方法我還沒弄明白到底是什么意思 適用于什么場景,也歡迎大神們給出個答案 謝謝

distinct方法允許你強(qiáng)制查詢返回不重復(fù)的結(jié)果集。

  public function getArticlesInfo()
  {
    $articles = DB::table('articles')->distinct()->get();
  }
1.5.3 addSelect方法

如果你想要添加一個select 可以這樣做:

  public function getArticlesInfo()
  {
    $query = DB::table('articles')->select('title as articles_title');
    $articles = $query->addSelect('id')->get();
    dd($articles);
  }

2 where語句

where語句是比較常用的,經(jīng)常用他來進(jìn)行條件篩選。

2.1 where基礎(chǔ)介紹

現(xiàn)在來詳細(xì)介紹下where方法 它接收三個參數(shù):

  1. 列名,這個沒什么好說的。

  2. 數(shù)據(jù)庫系統(tǒng)支持的操作符,比如說 ”=“、”<“、”like“這些,如果不傳入第二個參數(shù) 那么默認(rèn)就是”=“等于。

  3. 要比較的值。

  public function getArticlesInfo()
  {
    $articles1 = DB::table('articles')->where('id','2')->get();     // 等于
    $articles2 = DB::table('articles')->where('id','>','2')->get();   // 大于
    $articles3 = DB::table('articles')->where('id','<>','2')->get();  // 不等于
    $articles4 = DB::table('articles')->where('id','<=','2')->get();  // 小于等于
    $articles5 = DB::table('articles')->where('title','LIKE','%base')->get();  // 類似
  }

2.2 orWhere

orWhere和where接收的參數(shù)是一樣的,當(dāng)where邏輯沒有查找到 or查找到了 返回or的結(jié)果,當(dāng)where查找到了 or也查找到了 返回它們的結(jié)果。

  public function getArticlesInfo()
  {
    $articles = DB::table("articles")->where('id','=','5')->orWhere('title','laravel database')->get();
    dd($articles);
  }

2.3 whereBetween和whereNotBetween

whereBetween是指列值是否在所給定的值之間:

  public function getArticlesInfo()
  {
    $articles = DB::table("articles")->whereBetween('id', [1, 3])->get();
    dd($articles);
  }

↑ 上述代碼是查找id在1~3之間的集合。

whereNotBetween和whereBetween相反:

  public function getArticlesInfo()
  {
    $articles = DB::table("articles")->whereNotBetween('id', [1, 3])->get();
    dd($articles);
  }

↑ 上述代碼是查找id不在1~3之間的集合。

2.4 whereIn和whereNotIn

whereIn是查找列值在給定的一組數(shù)據(jù)中:

  public function getArticlesInfo()
  {
    $articles = DB::table("articles")->whereIn('id', [1, 3, 5, 8])->get();
    dd($articles);
  }

↑ 上述代碼中是查找ID為1,3,5,8的集合,不過我們數(shù)據(jù)庫中只有id為1和3的數(shù)據(jù) 那么它只會返回id為1和3的集合。

whereNotIn和whereIn相反的:

  public function getArticlesInfo()
  {
    $articles = DB::table("articles")->whereNotIn('id', [1, 3, 5, 8])->get();
    dd($articles);
  }

↑ 上述代碼中是查找ID不是1,3,5,8的集合。

2.5 whereNull和whereNotNull

whereNull是查找列值為空的數(shù)據(jù):

  public function getArticlesInfo()
  {
    $articles = DB::table("articles")->whereNull('created_at')->get();
    dd($articles);
  }

↑ 上述代碼中是查找created_at為空的集合。

whereNotNull就不用說啦:

  public function getArticlesInfo()
  {
    $articles = DB::table("articles")->whereNotNull('created_at')->get();
    dd($articles);
  }

↑ 上述代碼中是查找created_at不為空的集合。

3 插入數(shù)據(jù)

先看下最簡單的插入方法:

  public function getInsertArticle()
  {
    // 插入一條數(shù)據(jù):
    DB::table('articles')->insert(
      ['title'=>'get more', 'body'=>'emmmmmm......']
    );
    // 插入多條數(shù)據(jù):
    DB::table('articles')->insert([
      ['title'=>'testTitle1', 'body'=>'testBody1'],
      ['title'=>'testTitle2', 'body'=>'testBody2'],
      // ....
    ]);
  }

當(dāng)你需要拿到插入數(shù)據(jù)的ID的話,可以使用獲取自增ID的方法:

  public function getInsertArticle()
  {
    // 插入一條數(shù)據(jù):
    $id = DB::table('articles')->insertGetId(
      ['title'=>'get more', 'body'=>'emmmmmm......']
    );
    dd($id);
  }

4 更新

  public function getUpdateArticle()
  {
    $result = DB::table('articles')->whereBetween('id', [1, 3])->update(['comment_count'=>0]);
    dd($result);
  }

↑ update還可以返回影響了幾條數(shù)據(jù)。

4.1 加/減快捷方法

  public function getUpdateArticle()
  {
    $result = DB::table('articles')->whereBetween('id', [1, 3])->increment('comment_count',2);
    dd($result);
  }

↑ increment接受1~2個參數(shù),第一個參數(shù)是列名,第二個參數(shù)是可選的表示增加幾(默認(rèn)是1),上面的語句是:comment_count這一列的值增加2。

  public function getUpdateArticle()
  {
    $result = DB::table('articles')->whereBetween('id', [1, 3])->decrement('comment_count',2);
    dd($result);
  }

↑ decrement接受1~2個參數(shù),第一個參數(shù)是列名,第二個參數(shù)是可選的表示減少幾(默認(rèn)是1),上面的語句是:comment_count這一列的值減少2。

你以為加減快捷方法只接收兩個參數(shù)么?nonono 它還可以接收第三個參數(shù):

  public function getUpdateArticle()
  {
    $result = DB::table('articles')->whereBetween('id', [1, 3])->increment('comment_count', 2, ['title' => 'testUpdate']);
    dd($result);
  }

↑ 它還可以在增加/減少時(shí)對其他列進(jìn)行修改。

5 刪除

  public function getDeleteArticle()
  {
    $result = DB::table('articles')->whereBetween('id', [1, 3])->delete();
    dd($result);
  }

↑ 用delete刪除數(shù)據(jù),它也返回有多少行被影響。

當(dāng)你想要刪除所有的列 并且把自增ID歸0的話 可以這么做:

  public function getDeleteArticle()
  {
    DB::table('articles')->truncate();
  }

6 鎖

查詢構(gòu)建器還包含一些方法幫助你在select語句中實(shí)現(xiàn)”悲觀鎖“。可以在查詢中使用sharedLock方法從而在運(yùn)行語句時(shí)帶一把”共享鎖“。共享鎖可以避免被選擇的行被修改直到事務(wù)提交:

DB::table('articles')->where('id', '>', 100)->sharedLock()->get();

此外你還可以使用lockForUpdate方法?!眆or update“鎖避免選擇行被其它共享鎖修改或刪除:

DB::table('articles')->where('id', '>', 100)->lockForUpdate()->get();

上述內(nèi)容就是怎么在Laravel5.1 中使用查詢構(gòu)建器,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁題目:怎么在Laravel5.1中使用查詢構(gòu)建器-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://aaarwkj.com/article2/jcjoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、App開發(fā)、網(wǎng)站設(shè)計(jì)ChatGPT、手機(jī)網(wǎng)站建設(shè)服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名
国产免费很黄很色视频| 欧美日韩一级性生活片| 91欧美视频在线观看| 午夜草草视频在线观看| 91超碰这里只有精品国产| 亚洲av少妇一区二区成年男人 | 中日中文av一区二区三区| 亚洲狠狠爱一区二区三区| 日本精品人妻一区二区三区蜜桃| 精品亚洲一区二区三区| 亚洲日本国产一区二区| 丰满人妻一区二三区av| 日本加勒比不卡在线视频| 99在线视频午夜福利| 精品国产一区二区av麻豆| 亚洲一级特黄高清录像| 午夜福利成人在线免费观看| 白白色发布青青在线视频观看| 欧美私人影院—区二区日本| 国产婷婷色三区一区二区| 少妇人妻精品一区二区三| 国产精品午夜福利91| 日本高清一区二区高清| 午夜在线成人免费观看| 丝袜美腿蜜汁一龙二凤| 国产精品久久av高潮呻吟| 在线午夜免费视频观看| 97水蜜桃视频在线观看| 最新日本免费久久精品| 国产一区二区av免费| 日韩成人大片在线播放| 禁止18岁以下的视频| 国产自拍偷拍自拍偷拍| 国产婷婷精品一区二区| 国产日韩亚洲欧美在线| 精品亚洲第一区二区免费在线| 国产日韩久久免费电影| 水牛av影视亚洲精品| 午夜体内射精免费视频| 日本不卡高清视频在线播放| 亚洲性码不卡视频在线|