亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Laravel框架查詢構(gòu)造器 CURD操作示例

 更新時間:2019年09月04日 11:00:51   作者:doubly_yi  
這篇文章主要介紹了Laravel框架查詢構(gòu)造器 CURD操作,結(jié)合實例形式分析了Laravel框架使用查詢構(gòu)造器進(jìn)行CURD操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了Laravel框架查詢構(gòu)造器 CURD操作。分享給大家供大家參考,具體如下:

新增

//插入一條數(shù)據(jù)
public function insert(){
  $rs = DB::table('student')->insert([
    'name' => 'Kit',
    'age' => 12
  ]);
  dd($rs);  //true
}

//插入一條數(shù)據(jù)并返回自增ID
public function insert(){
  $id = DB::table('student')->insertGetId([
    'name'=>'Tom',
    'age'=>11
  ]);
  dd($id);  //1004
}

//插入多條數(shù)據(jù)
public function insert(){
  $rs = DB::table('student')->insert([
    ['name'=>'Ben','age'=>22],
    ['name'=>'Jean','age'=>23]
  ]);
  dd($rs);//true
}

更新

//更新一條數(shù)據(jù)
public function update(){
  $rs = DB::table('student')
    ->where('id',1003)
    ->update(['age'=>10]);
  dd($rs);//1,返回受影響的行數(shù)
}

//自增更新
public function update(){
  //所有年齡加1
  $rs = DB::table('student')->increment('age');
  dd($rs);//5,返回受影響的行數(shù)
  //ID為1001的年齡加3
  $rs = DB::table('student')
    ->where('id',1001)
    ->increment('age',3);
  dd($rs);//1,返回受影響的行數(shù)
}

//自減更新
public function update(){
  //所有年齡加1
  $rs = DB::table('student')->decrement('age');
  dd($rs);//5,返回受影響的行數(shù)
  //ID為1001的年齡加3
  $rs = DB::table('student')
    ->where('id',1001)
    ->decrement('age',3);
  dd($rs);//1,返回受影響的行數(shù)
}

//1001年齡加3并且性別改為11
public function update(){
  $rs = DB::table('student')
    ->where('id',1001)
    ->increment('age',3,['sex'=>11]);
  dd($rs);//1,返回受影響的行數(shù)
}

刪除

//刪除ID為1006的數(shù)據(jù)
public function delete(){
  $rs = DB::table('student')
    ->where('id',1006)
    ->delete();
  dd($rs);//1,返回受影響的行數(shù)
}

//刪除ID大于1003的數(shù)據(jù)
public function delete(){
  $rs = DB::table('student')
    ->where('id','>',1003)
    ->delete();
  dd($rs);//2,返回受影響的行數(shù)
}

//清空數(shù)據(jù)表,不返回任何東西
DB::table('student')->truncate();

查詢

  • get
  • first
  • pluck
  • select
//查詢所有數(shù)據(jù)
$rs = DB::table('student')->get();

//查詢第一條數(shù)據(jù)
$rs = DB::table('student')->orderBy('id','desc')->first();

//查詢一個name字段
$rs = DB::table('student')->pluck('name');
//查詢name字段并以ID為鍵名
$rs = DB::table('student')->pluck('name','id');

//查詢name,age,sex字段
$rs = DB::table('student')->select('name','age','sex')->get();

聚合函數(shù)

$rs = DB::table('student')->count();
$rs = DB::table('student')->max('age');
$rs = DB::table('student')->min('age');
$rs = DB::table('student')->avg('age');
$rs = DB::table('student')->sum('age');

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

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

相關(guān)文章

最新評論