日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

十六、PHP框架Laravel学习笔记——构造器的增删改

發布時間:2024/7/5 php 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 十六、PHP框架Laravel学习笔记——构造器的增删改 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一.增刪改操作

  • 使用 insert()方法可以新增一條或多條記錄;
  • //新增一條記錄 DB::table('users')->insert([ 'username' => '李白', 'password' => '123456', 'email' => 'libai@163.com', 'details' => '123' ]); //新增多條記錄 DB::table('users')->insert([ [...], [...] ]);




  • 使用 insertOrIgnore()方法,可以忽略重復插入數據的錯誤;
  • //忽略重復新增數據的錯誤 DB::table('users')->insertOrIgnore([ 'id' => 304, 'username' => '李白', 'password' => '123456', 'email' => 'libai@163.com', 'details' => '123' ]);
  • 使用 insertGetId()方法,獲取新增后的自增 ID;
  • //獲取新增后返回的 ID $id = DB::table('users')->insertGetId([ 'username' => '李白', 'password' => '123456', 'email' => 'libai@163.com', 'details' => '123' ]); return $id;


  • 使用 update()方法,可以通過條件更新一條數據內容;
  • //更新修改一條數據 DB::table('users')->where('id', 304) ->update([ 'username' => '李紅', 'email' => 'lihong@163.com' ]);


  • 使用 updateOrInsert()方法,可以先進行查找修改,如不存在,則新增;
  • //參數 1:修改的條件 //參數 2:修改的內容(新增的內容) DB::table('users')->updateOrInsert( ['id'=>307], ['username'=>'李黑', 'password'=>'654321', 'details'=>'123'] );


  • 對于 json 數據,新增和修改的方法和正常數據類似;
  • //新增時,轉換為 json 數據 'list' => json_encode(['id'=>19]) //修改時,使用 list->id 指定 DB::table('users')->where('id', 306) ->update([ 'list->id' => 20 ]);

  • 更新數據時,可以使用自增 increment()和自減 decrement()方法;
  • //默認自增/自減為 1,可設置 DB::table('users')->where('id', 306)->increment('price'); DB::table('users')->where('id', 306)->increment('price', 2);

  • 使用 delete()刪除數據,一般來說要加上 where 條件,否則清空;
  • //刪除一條數據 DB::table('users')->delete(307); DB::table('users')->where('id', 307)->delete(); //清空 DB::table('users')->delete(); DB::table('users')->truncate();




    總結

    以上是生活随笔為你收集整理的十六、PHP框架Laravel学习笔记——构造器的增删改的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。