laravel架构设置(Laravel框架查询构造器 CURD操作示例)
类别:编程学习 浏览量:2074
时间:2021-10-24 10:30:16 laravel架构设置
Laravel框架查询构造器 CURD操作示例本文实例讲述了Laravel框架查询构造器 CURD操作。分享给大家供大家参考,具体如下:
新增
|
//插入一条数据 public function insert(){ $rs = DB::table( 'student' )->insert([ 'name' => 'Kit' , 'age' => 12 ]); dd( $rs ); //true } |
|
//插入一条数据并返回自增ID public function insert(){ $id = DB::table( 'student' )->insertGetId([ 'name' => 'Tom' , 'age' =>11 ]); dd( $id ); //1004 } |
|
//插入多条数据 public function insert(){ $rs = DB::table( 'student' )->insert([ [ 'name' => 'Ben' , 'age' =>22], [ 'name' => 'Jean' , 'age' =>23] ]); dd( $rs ); //true } |
更新
|
//更新一条数据 public function update(){ $rs = DB::table( 'student' ) ->where( 'id' ,1003) ->update([ 'age' =>10]); dd( $rs ); //1,返回受影响的行数 } |
|
//自增更新 public function update(){ //所有年龄加1 $rs = DB::table( 'student' )->increment( 'age' ); dd( $rs ); //5,返回受影响的行数 //ID为1001的年龄加3 $rs = DB::table( 'student' ) ->where( 'id' ,1001) ->increment( 'age' ,3); dd( $rs ); //1,返回受影响的行数 } |
|
//自减更新 public function update(){ //所有年龄加1 $rs = DB::table( 'student' )->decrement( 'age' ); dd( $rs ); //5,返回受影响的行数 //ID为1001的年龄加3 $rs = DB::table( 'student' ) ->where( 'id' ,1001) ->decrement( 'age' ,3); dd( $rs ); //1,返回受影响的行数 } |
|
//1001年龄加3并且性别改为11 public function update(){ $rs = DB::table( 'student' ) ->where( 'id' ,1001) ->increment( 'age' ,3,[ 'sex' =>11]); dd( $rs ); //1,返回受影响的行数 } |
删除
|
//删除ID为1006的数据 public function delete (){ $rs = DB::table( 'student' ) ->where( 'id' ,1006) -> delete (); dd( $rs ); //1,返回受影响的行数 } |
|
//删除ID大于1003的数据 public function delete (){ $rs = DB::table( 'student' ) ->where( 'id' , '>' ,1003) -> delete (); dd( $rs ); //2,返回受影响的行数 } |
|
//清空数据表,不返回任何东西 DB::table( 'student' )->truncate(); |
查询
- get
- first
- pluck
- select
|
//查询所有数据 $rs = DB::table( 'student' )->get(); |
|
//查询第一条数据 $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(); |
聚合函数
|
$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' ); |
希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。
原文链接:https://blog.csdn.net/qq_18335837/article/details/81287841
您可能感兴趣
- laravel数据表配置(laravel实现按月或天或小时统计mysql数据的方法)
- laravel模型获取表名(Laravel 对某一列进行筛选然后求和sum的例子)
- laravel关联查询限制数量(laravel-admin 后台表格筛选设置默认的查询日期方法)
- laravel的api验证失败重定向(Laravel的Auth验证Token验证使用自定义Redis的例子)
- laravel数据查询(laravel按天、按小时,查询数据的实例)
- laravel模型个数(laravel withCount 统计关联数量的方法)
- laravel测试重连数据库(Laravel关系模型指定条件查询方法)
- laravel终止函数执行(laravel 输出最后执行sql 附:whereIn的使用方法)
- laravel数据库操作方式(Laravel 实现数据软删除功能)
- laravel模块教程(Laravel框架创建路由的方法详解)
- laravel count 报错(解决laravel5.5访问public报错的问题)
- laravel后台登录教程(Laravel 默认邮箱登录改成用户名登录的实现方法)
- laravelqq邮箱发送失败(解决laravel 表单提交-POST 异常的问题)
- laravel自定义命令(laravel-admin select框默认选中的方法)
- laravel认证系统(Laravel框架Auth用户认证操作实例分析)
- laravel设置读写权限(解决laravel上传图片之后,目录有图片,但是访问不到404的问题)
- 冬天来了手脚冰凉 真不是因为上辈子你是折翼的天使(冬天来了手脚冰凉)
- 0 1 岁婴儿最强作息指南,照着做养出天使宝宝(01岁婴儿最强作息指南)
- 沪上这16所高校 萌新 礼包开箱 哪一款让你心动(沪上这16所高校萌新)
- 她救了被绑架的他,而这一切竟是一场阴谋...(她救了被绑架的他)
- 冬季养殖这6种阴生植物,方便又好养,你家有么(冬季养殖这6种阴生植物)
- 阴生植物为什么不怕照不到阳光(阴生植物为什么不怕照不到阳光)
热门推荐
- sql两列数据快速对比(在sql中对两列数据进行运算作为新的列操作)
- nginx服务器怎么屏蔽爬虫(nginx 防盗链防爬虫配置详解)
- html5的文件类型声明(浅析HTML5中的download属性使用)
- vue2和vue3都如何创建项目(vue3.0+vite2实现动态异步组件懒加载)
- jquery滚动固定插件在哪(jquery 步骤进度轴插件的实现代码)
- 如何在阿里云虚拟主机部署网站(阿里云云虚拟主机更改操作系统流程)
- sqlserver 高级查询(SQL Server2019数据库之简单子查询的具有方法)
- SQL Server中的临时表的用法
- dedecms标签缩略图问题(dedecms文章内页获取缩略图的调用标签)
- dedecms标签调用原理(DEDECMS栏目页开伪静态的方法apache服务器)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9