新增
//插入一条数据
public
function
insert(){
$rs
= DB::table(
'student'
)->insert([
'name'
=>
'Kit'
,
'age'
=> 12
]);
dd(
);
//true
}
//插入一条数据并返回自增ID
$id
)->insertGetId([
'Tom'
=>11
//1004
//插入多条数据
[
'Ben'
=>22],
'Jean'
=>23]
更新
//更新一条数据
update(){
)
->where(
'id'
,1003)
->update([
=>10]);
//1,返回受影响的行数
//自增更新
//所有年龄加1
)->increment(
//5,返回受影响的行数
//ID为1001的年龄加3
,1001)
->increment(
,3);
//自减更新
)->decrement(
->decrement(
//1001年龄加3并且性别改为11
,3,[
'sex'
=>11]);
删除
//删除ID为1006的数据
delete
(){
,1006)
->
();
//删除ID大于1003的数据
'>'
//2,返回受影响的行数
//清空数据表,不返回任何东西
DB::table(
)->truncate();
查询
//查询所有数据
)->get();
//查询第一条数据
)->orderBy(
'desc'
)->first();
//查询一个name字段
)->pluck(
//查询name字段并以ID为键名
//查询name,age,sex字段
)->select(
聚合函数
)->
count
)->max(
)->min(
)->avg(
)->sum(