欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Laravel框架Eloquent ORM簡介、模型建立及查詢數(shù)據(jù)操作詳解

 更新時間:2019年12月04日 11:28:19   作者:陳柴Rarin  
這篇文章主要介紹了Laravel框架Eloquent ORM簡介、模型建立及查詢數(shù)據(jù)操作,結(jié)合實例形式詳細分析了laravel框架Eloquent ORM基本概念、原理、模型建立及數(shù)據(jù)查詢等相關使用技巧,需要的朋友可以參考下

本文實例講述了Laravel框架Eloquent ORM簡介、模型建立及查詢數(shù)據(jù)操作。分享給大家供大家參考,具體如下:

注:以下知識點可能有不全面之處,望見諒

NO.1Eloquent ORM簡介

Laravel所自帶的Eloquent ORM是一個優(yōu)美、簡潔的ActiveRecord實現(xiàn),用來實現(xiàn)數(shù)據(jù)庫操作
每個數(shù)據(jù)表都有與之相對應的“模型(Model)”用于和數(shù)據(jù)交互

NO.2模型的建立

最基礎的模型代碼如下:

namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
 //指定表名
 protected $table = 'student';
 //指定id
 protected $primaryKey = 'id';
}

將他創(chuàng)建于app目錄下,命名為Student.php

NO.3查詢數(shù)據(jù)

首先在查詢之前,我先讓你們看一下我的數(shù)據(jù)庫

數(shù)據(jù)如上,然后查詢

1.all方式

代碼如下:

namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
 public function orm1()
  {
      $students = Student::all();
      dd($students);
 }
}

顯示數(shù)據(jù)庫里的所有數(shù)據(jù)

2.find方式

代碼如下:

namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
 public function orm1()
  {
      $students = Student::find(1);
      dd($students);
 }
}

查找指定數(shù)據(jù)

3.findOrFail方式

代碼如下:

namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
 public function orm1()
  {
      $students = Student::findOrFail(1);
      dd($students);
 }
}

如果他沒查到指定的數(shù)據(jù),那么他會報錯,而find若是沒有查到該函數(shù),只會彈出一個null

4.查詢構(gòu)造器的使用

  • 1.get方式使用
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
 public function orm1()
  {
      $students = Student::get();
      dd($students);
 }
}

他會得到一個完整的數(shù)據(jù)信息,和原本的意義沒有區(qū)別

  • 2.first方式使用

代碼如下:

namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
 public function orm1()
  {
     $student = Student::where('id','>',1)
     ->orderBy('age','desc')
     ->first();
     dd($student);
 }
}

當id大于一的時候,獲取一個最大值的age

  • 3.where方式使用

代碼如下:

namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
 public function orm1()
  {
     $student = Student::where('id','>',1)
     ->get();
     dd($student);
 }
}

  • 4.chunk方式使用

代碼如下:

namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
 public function orm1()
  {
     $student = Student::chunck(2,function($student){
  var_dump($student);
 });
 }
}

5.聚合函數(shù)的使用

  • 1.count函數(shù)

代碼如下:

namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
 public function orm1()
  {
     $student = Student::count();
     dd($student);
 }
}

  • 2.max函數(shù)

代碼如下:

namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
 public function orm1()
  {
     $student = Student::max('age');
     dd($student);
 }
}

  • 3.min函數(shù)

代碼如下:

namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
 public function orm1()
  {
     $student = Student::min('age');
     dd($student);
 }
}

  • 4.avg函數(shù)

代碼如下:

namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
 public function orm1()
  {
     $student = Student::avg('age');
     dd($student);
 }
}

  • 5.sum函數(shù)

代碼如下:

namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
 public function orm1()
  {
     $student = Student::sum('age');
     dd($student);
 }
}

更多關于Laravel相關內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家基于Laravel框架的PHP程序設計有所幫助。

相關文章

  • 基于ThinkPHP5框架使用QueryList爬取并存入mysql數(shù)據(jù)庫操作示例

    基于ThinkPHP5框架使用QueryList爬取并存入mysql數(shù)據(jù)庫操作示例

    這篇文章主要介紹了基于ThinkPHP5框架使用QueryList爬取并存入mysql數(shù)據(jù)庫操作,結(jié)合實例形式分析了thinkPHP5框架整合QueryList爬取數(shù)據(jù)存入mysql相關操作技巧及注意事項,需要的朋友可以參考下
    2019-05-05
  • php 購物車的例子

    php 購物車的例子

    網(wǎng)上搜到的,簡單容易理解。cookie存購物車ID,db存購物車數(shù)據(jù)。
    2009-05-05
  • PHP頁面輸出搜索后跳轉(zhuǎn)下一頁的處理方法

    PHP頁面輸出搜索后跳轉(zhuǎn)下一頁的處理方法

    這篇文章主要介紹了PHP頁面輸出搜索后跳轉(zhuǎn)下一頁的處理方法,用js來給url加上搜索的條件,保證跳轉(zhuǎn)下一頁時輸出的是搜索到的數(shù)據(jù),對實現(xiàn)代碼感興趣的朋友可以參考下本文
    2016-09-09
  • zend framework框架中url大小寫問題解決方法

    zend framework框架中url大小寫問題解決方法

    這篇文章主要介紹了zend framework框架中url大小寫問題解決方法,包括action控制器和請求URL的大小寫,需要的朋友可以參考下
    2014-08-08
  • PHP http請求超時問題解決方案

    PHP http請求超時問題解決方案

    這篇文章主要介紹了PHP http請求超時問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-11-11
  • PHP命名空間(Namespace)的使用詳解

    PHP命名空間(Namespace)的使用詳解

    命名空間一個最明確的目的就是解決重名問題,PHP中不允許兩個函數(shù)或者類出現(xiàn)相同的名字,否則會產(chǎn)生一個致命的錯誤。這種情況下只要避免命名重復就可以解決
    2013-05-05
  • Yii2基于Ajax自動獲取表單數(shù)據(jù)的方法

    Yii2基于Ajax自動獲取表單數(shù)據(jù)的方法

    這篇文章主要介紹了Yii2基于Ajax自動獲取表單數(shù)據(jù)的方法,涉及Yii結(jié)合ajax調(diào)用鼠標事件動態(tài)查詢表單的相關技巧,需要的朋友可以參考下
    2016-08-08
  • yii2多圖上傳組件的使用教程

    yii2多圖上傳組件的使用教程

    這篇文章主要介紹了yii2多圖上傳組件的使用教程,在使用過程中有許多注意要點,下面小編給大家分享到腳本之家平臺,需要的朋友可以參考下
    2018-05-05
  • PHP時間戳 strtotime()使用方法和技巧

    PHP時間戳 strtotime()使用方法和技巧

    php strtotime()解釋如何使用,看了下面的文章就要以學習到了。下面還有php文檔函數(shù)解釋。
    2013-10-10
  • CI(CodeIgniter)框架中URL特殊字符處理與SQL注入隱患分析

    CI(CodeIgniter)框架中URL特殊字符處理與SQL注入隱患分析

    這篇文章主要介紹了CI(CodeIgniter)框架中URL特殊字符處理與SQL注入隱患,結(jié)合實例形式分析了CodeIgniter框架中針對特殊字符的過濾及SQL注入隱患的相關原理,需要的朋友可以參考下
    2019-02-02

最新評論