laravel对数据库进行增删改查,一学就会

2021-02-1316:48:32后端程序开发Comments1,490 views字数 965阅读模式

laravel对数据库进行增删改查,其实很简单,一学就会。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

laravel对数据库进行增删改查,一学就会文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

在上一篇的文章中我们学会了使用facade来对数据库进行增删改查,这种方法比较没那么灵活,也不是很方便,今天我们来介绍另一种laravel对数据库增删改查的方法,那就是通过查询构造器来实现。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

laravel对数据库进行增删改查,一学就会文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

新增数据文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

使用insert来进行,如果要想在插入数据成功之后,返回id就用insertGetId。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

例子:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

#查询构造器之增加数据之后返回自增的id文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

$result=DB::table("students")->insertGetId(['name'=>"tianli","age"=>23,"register"=>$time]);//为students这个表新增一条数据并返回id文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

var_dump($result);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

laravel对数据库进行增删改查,一学就会文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

更新数据文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

更新数据,用到的是update这个关键字,在更新的时候,可以带上条件查询where。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

例子如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

#查询构造器之更新数据文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

$result=DB::table("students")->where('id',2)->update(["name"=>"luoshan"]);//对students这个表中id为2的这条记录进行更新,将name这个字段更新为luoshan文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

var_dump($result);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

laravel对数据库进行增删改查,一学就会文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

查询数据文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

查询数据用到的是get这个关键字,例子如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

#查询数据信息文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

$result=DB::table("students")->where("id",4)->get();//查询students这个表中id为4的这条记录。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

var_dump($result);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

laravel对数据库进行增删改查,一学就会文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

删除数据文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

在laravel查询构造器中用到删除数据的关键字是delete,例子如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

#删除数据文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

$bool=DB::table("students")->where("id",3)->delete();//删除students这个表中id为3的记录文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

var_dump($bool);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

自增自减文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

自增自在更新数据的时候,我们还可以对某些字段的内容进行自增或者四自减,例子如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

#自增自减带条件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

$bool=DB::table("students")->where('id',">=",3)->increment("age",3);//increment表示的是增加,decrement表示自减文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

var_dump($bool);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/20947.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/20947.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定