登录  /  注册
首页 > php教程 > PHP开发 > 正文

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

高洛峰
发布: 2016-11-22 12:40:47
原创
1756人浏览过

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

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

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