Laravel框架数据库CURD操作、连贯操作使用方法

千涛君_7483

千涛君_7483

2016-11-22

2053人浏览

原创

laravel框架数据库curd操作、连贯操作使用方法
laravel框架数据库curd操作、连贯如何来操作了这个操作性是非常的方便简单了我们在这里来为各位介绍一篇相关的教程,具体的细节步骤如下文介绍。
laravel是一套简洁、优雅的php web开发框架(php web framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络app,而且每行代码都可以简洁、富于表达力。
一、selects
检索表中的所有行
$users = db::table('users')->get();
foreach ($users as $user)
{
var_dump($user->name);
}
从表检索单个行
$user = db::table('users')->where('name', 'john')->first();
var_dump($user->name);
检索单个列的行
$name = db::table('users')->where('name', 'john')->pluck('name');
检索一个列值列表
$roles = db::table('roles')->lists('title');
该方法将返回一个数组标题的作用。你也可以指定一个自定义的键列返回的数组
$roles = db::table('roles')->lists('title', 'name');
 
指定一个select子句
$users = db::table('users')->select('name', 'email')->get();
$users = db::table('users')->distinct()->get();
$users = db::table('users')->select('name as user_name')->get();
select子句添加到一个现有的查询
$query = db::table('users')->select('name');
$users = $query->addselect('age')->get();
where
$users = db::table('users')->where('votes', '>', 100)->get();
or
$users = db::table('users')->where('votes', '>', 100)->orwhere('name', 'john')->get();
 
where between
$users = db::table('users')->wherebetween('votes', array(1, 100))->get();
 
where not between
$users = db::table('users')->wherenotbetween('votes', array(1, 100))->get();
 
where in with an array
$users = db::table('users')->wherein('id', array(1, 2, 3))->get();
$users = db::table('users')->wherenotin('id', array(1, 2, 3))->get();
 
using where null to find records with unset values
$users = db::table('users')->wherenull('updated_at')->get();
 
order by, group by, and having
$users = db::table('users')->orderby('name', 'desc')->groupby('count')->having('count', '>', 100)->get();
offset & limit
$users = db::table('users')->skip(10)->take(5)->get();
二、连接
joins
查询构建器也可以用来编写连接语句。看看下面的例子:
db::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.id', 'contacts.phone', 'orders.price')
->get();
左连接语句
db::table('users')
->leftjoin('posts', 'users.id', '=', 'posts.user_id')
->get();
db::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')->oron(...);
})
->get();
db::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);
})
->get();
 
三、分组
有时候,您可能需要创建更高级的where子句,如“存在”或嵌套参数分组。laravel query builder可以处理这些:
db::table('users')
->where('name', '=', 'john')
->orwhere(function($query)
{
$query->where('votes', '>', 100)
->where('title', '', 'admin');
})
->get();
 
上面的查询将产生以下sql:
select * from users where name = 'john' or (votes > 100 and title
'admin')
exists statements
db::table('users')
->whereexists(function($query)
{
$query->select(db::raw(1))
->from('orders')
->whereraw('orders.user_id = users.id');
})
->get();
上面的查询将产生以下sql:
select * from userswhere exists (
select 1 from orders where orders.user_id = users.id
)
 
四、聚合
查询构建器还提供了各种聚合方法,如统计,马克斯,min,avg和总和。
$users = db::table('users')->count();
$price = db::table('orders')->max('price');
$price = db::table('orders')->min('price');
$price = db::table('orders')->avg('price');
$total = db::table('users')->sum('votes');
raw expressions
有时您可能需要使用一个原始表达式的查询。这些表达式将注入的查询字符串,所以小心不要创建任何sql注入点!创建一个原始表达式,可以使用db:rawmethod:
using a raw expression
$users = db::table('users')
->select(db::raw('count(*) as user_count, status'))
->where('status', '', 1)
->groupby('status')
->get();
 
递增或递减一个列的值

DB::table('users')->increment('votes');
DB::table('users')->increment('votes', 5);
DB::table('users')->decrement('votes');
DB::table('users')->decrement('votes', 5);
 
您还可以指定额外的列更新:

DB::table('users')->increment('votes', 1, array('name' => 'John'));
 
Inserts
将记录插入表
DB::table('users')->insert(
array('email' => 'john@example.com', 'votes' => 0)
);
 
将记录插入表自动增加的ID
如果表,有一个自动递增的id字段使用insertGetId插入一个记录和检索id:

$id = DB::table('users')->insertGetId(
array('email' => 'john@example.com', 'votes' => 0)
);

 
注意:当使用PostgreSQL insertGetId方法预计,自增列被命名为“id”。
多个记录插入到表中
代码如下:
DB::table('users')->insert(array(
array('email' => 'taylor@example.com', 'votes' => 0),
array('email' => 'dayle@example.com', 'votes' => 0),
));
 
四、Updates
更新一个表中的记录
代码如下:
DB::table('users')
->where('id', 1)
->update(array('votes' => 1));
 
五、 Deletes
删除表中的记录
代码如下:
DB::table('users')->where('votes', 'delete();
 
删除表中的所有记录
代码如下:
DB::table('users')->delete();
删除一个表
 
代码如下:
 
DB::table('users')->truncate();
 
六、Unions
查询构建器还提供了一种快速的方法来“联盟”两个查询:
 
代码如下:
$first = DB::table('users')->whereNull('first_name');
$users =
DB::table('users')->whereNull('last_name')->union($first)->get();
 
unionAll方法也可以,有相同的方法签名。
Pessimistic Locking
查询构建器包括一些“悲观锁定”功能来帮助你做你的SELECT语句。  运行SELECT语句“共享锁”,你可以使用sharedLock方法查询:
代码如下:
DB::table('users')->where('votes', '>',
100)->sharedLock()->get();
更新“锁”在一个SELECT语句,您可以使用lockForUpdate方法查询:
 
代码如下:
 
DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();
 
七、缓存查询
你可以轻松地缓存查询的结果使用记忆法:
代码如下:
$users = DB::table('users')->remember(10)->get();
在本例中,查询的结果将为十分钟被缓存。查询结果缓存时,不会对数据库运行,结果将从默认的缓存加载驱动程序指定您的应用程序。  如果您使用的是支持缓存的司机,还可以添加标签来缓存:
 
代码如下:
 
$users = DB::table('users')->cacheTags(array('people', 'authors'))->remember(10)->get();

Laravel Cloud
Laravel Cloud

Laravel Cloud(cloud.laravel.com)。此技能用于处理所有 Laravel Cloud 请求,包括搜索和读取数据。只要任务涉及 Laravel Cloud,即使用此技能。

下载


相关文章

PHP速学视频免费教程(入门到精通)
PHP速学视频免费教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

laravel

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
GoFrame框架Web开发与路由中间件
GoFrame框架Web开发与路由中间件

本专题聚焦GoFrame框架的Web开发,拆解路由注册与匹配规则、请求参数获取、参数校验与中间件执行顺序,说明统一响应、JWT鉴权、文件上传与接口文档生成的落地方式,并补充优雅重启与测试实践。

2026.11.17

0

15

GoFrame框架安装与gf命令行工具
GoFrame框架安装与gf命令行工具

本专题围绕GoFrame框架的安装与工具链配置展开,讲清Go版本要求、gf命令行的安装方式、代理镜像与依赖准备,说明项目初始化、热编译、交叉编译打包的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.17

0

15

GoFrame框架零基础入门
GoFrame框架零基础入门

本专题整理GoFrame框架的入门知识,从模块化设计理念、环境准备到跑通第一个Web服务,讲清目录分层、配置加载、日志与中间件的用法,并对比Gin、Go-Zero的差异,帮助Go开发者建立整体认知。

2026.11.17

0

15

SQLAlchemy 一对多关系怎么建立
SQLAlchemy 一对多关系怎么建立

SQLAlchemy 一对多关系怎么建立?本专题整理父子表外键、relationship 双向映射、关联查询、级联删除和加载优化等相关文章,帮助你理解并实现常见的 ORM 关系建模。

2026.11.17

0

10

SQLAlchemy 如何增删改查
SQLAlchemy 如何增删改查

SQLAlchemy 如何增删改查?本专题整理 ORM 新增记录、条件查询、更新和删除、Session 事务提交与批量处理等实用文章,帮助你掌握 Python 数据库操作流程。

2026.11.17

0

11

SQLAlchemy ORM 模型怎么写
SQLAlchemy ORM 模型怎么写

SQLAlchemy ORM 模型怎么写?本专题整理模型类定义、Mapped 字段映射、主键与约束、表结构创建和常见报错的相关文章,帮助你从基础配置到实际建模逐步掌握 SQLAlchemy。

2026.11.17

0

16

Go-Zero API开发使用教程
Go-Zero API开发使用教程

本专题聚焦Go-Zero的API开发,拆解.api描述文件的语法结构、路由与分组定义、参数绑定与校验规则,说明统一响应体、JWT鉴权、跨域与接口文档生成的落地方式,并给出网关聚合与接口测试的实践建议。

2026.11.12

160

15

Go-Zero安装与goctl环境配置教程
Go-Zero安装与goctl环境配置教程

本专题围绕Go-Zero的安装与工具链配置展开,讲清Go版本要求、goctl安装方式、代理镜像、protoc依赖与环境变量设置,说明多版本共存、升级回退与CI流水线安装的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.12

120

15

Go-Zero零基础入门教程
Go-Zero零基础入门教程

本专题整理Go-Zero的入门知识,从框架定位、环境准备到跑通第一个API服务,讲清目录分层、配置加载、日志与中间件的基本用法,并对比Gin、Kratos的差异,帮助Go开发者建立整体认知,判断它是否适合当前项目。

2026.11.12

100

15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
如何安装 Composer
如何安装 Composer

共1课时 | 232人学习

Composer手册
Composer手册

共0课时 | 0人学习