Laravel框架查詢構(gòu)造器CURD操作-創(chuàng)新互聯(lián)

Laravel框架查詢構(gòu)造器 CURD操作?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)建站自2013年起,先為邵武等服務(wù)建站,邵武等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為邵武企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

新增


//插入一條數(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');

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。

本文標(biāo)題:Laravel框架查詢構(gòu)造器CURD操作-創(chuàng)新互聯(lián)
文章來源:http://www.muchs.cn/article42/hgcec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、網(wǎng)站改版、小程序開發(fā)、全網(wǎng)營銷推廣企業(yè)網(wǎng)站制作、微信小程序

廣告

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

成都網(wǎng)站建設(shè)