0

0

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

高洛峰

高洛峰

发布时间:2016-11-22 12:40:47

|

2033人浏览过

|

来源于php中文网

原创

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();

相关标签:

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

相关专题

更多
php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

7

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

4

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

7

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

7

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

42

2025.12.31

html5怎么播放视频
html5怎么播放视频

想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。

4

2025.12.31

关闭win10系统自动更新教程大全
关闭win10系统自动更新教程大全

本专题整合了关闭win10系统自动更新教程大全,阅读专题下面的文章了解更多详细内容。

3

2025.12.31

阻止电脑自动安装软件教程
阻止电脑自动安装软件教程

本专题整合了阻止电脑自动安装软件教程,阅读专题下面的文章了解更多详细教程。

3

2025.12.31

html5怎么使用
html5怎么使用

想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!

2

2025.12.31

热门下载

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

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Laravel---API接口
Laravel---API接口

共7课时 | 0.6万人学习

PHP自制框架
PHP自制框架

共8课时 | 0.6万人学习

PHP面向对象基础课程(更新中)
PHP面向对象基础课程(更新中)

共12课时 | 0.6万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号