laravel CURD
检索一个列值列表
DB::table("tablename")->lists('mobile'); //5.3 及以上版本 lists 改为 pluck
返回
[
"13455556666",
"13455556667",
"13455556668",
"13455556669",
]
指定一个自定义的键列返回的数组
\DB::table('tablename')->lists('mobile','email');
返回
[
"aa@sina.com"=>"13022223335",
"bb@sina.com"=>"13022223336",
"cc@sina.com"=>"13022223337",
]
检索表中的所有行
$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);
检索单行单列--返回指定字段
DB::table('users')->where('name', 'John')->pluck('name');
返回数组 非字符串
["Jone"] 并不是返回 "Jone"
指定一个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();
where
$users = DB::table('users')->where('votes', '>', 100)->get();
$users = DB::table('users')->where(['votes'=>100,'name'=>'zhangsan'])->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')->where('parent_id',1)->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();
//on 多个条件
DB::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')->on(...);
})
->get();
DB::table('users') ->join('contacts', function($join) { $join->on('users.id', '=', 'contacts.user_id') ->where('contacts.user_id', '>', 5); }) ->get();
聚合
$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');
递增或递减一个列的值
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 = DB::table('users')->insertGetId(
array('email' => 'john@example.com', 'votes' => 0)
); 多个记录插入到表中 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', '<', 100)->delete(); 删除表中的所有记录 DB::table('users')->delete(); 清空一个表 DB::table('users')->truncate();
共享锁
DB::table('users')->where('votes', '>',
100)->sharedLock()->get(); 更新“锁”
DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();
\DB::connection()->getPdo()->exec($sql)
事务 使用匿名函数 任何一个异常都会触发回滚
return \DB::transaction(function() use($user_params, $staff_params) {
//写管理员进入user表
$user_id = DB::table(User::TABLE_NAME)
->insertGetId($user_params);
if(!$user_id) throw new Exception("写入user表失败");
$staff_params[Staff::DB_FIELD_USER_ID] = $user_id;
$staff_id=DB::table(Staff::TABLE_NAME)
->insertGetId($staff_params);
if(!$staff_id) throw new Exception("写入staff表失败");
return $staff_id;
});
直接使用语句
$ret = DB::select("select * from tablename where id=123"); //返回一个二维数组
或者
$ret = DB::select("select * from tablename where id=?",[123]); //返回一个二维数组
使用命名绑定来执行查询
$results = DB::select('select * from users where id = :id', ['id' => 1]);
DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle']);
$affected = DB::update('update users set votes = 100 where name = ?', ['John']);
$deleted = DB::delete('delete from users');
DB::statement('drop table users');
//chunk()每次查n条
$student=DB::table("vipinfo")->chunk(2,function($students){ //每次查2条
var_dump($students);
if(.......) return false; //在满足某个条件下使用return就不会再往下查了
});
打印sql
\DB::connection()->enableQueryLog();#开启log
$aa = \DB::table(tablename)->first();
$log = \DB::getQueryLog();
dd($log);
//打印
array: [
=> array: [
"query" => "select * from `fuli_xwc_merchant` limit 1"
"bindings" => []
"time" => 29.27
]
]
手动使用事务
如果你想要手动开始事务从而对回滚和提交有一个完整的控制,可以使用 DB 门面的beginTransaction 方法: DB::beginTransaction();
你可以通过 rollBack 方法回滚事务: DB::rollBack();
最后,你可以通过 commit 方法提交事务: DB::commit();
注意:使用 DB 门面的事务方法还可以用于控制查询构建器和 Eloquent ORM 的事务。
like
$users = DB::table('users')
->where('name', 'like', 'T%')
->get();
$users = DB::table('users')
->where('votes', '>=', 100)
->get();
$users = DB::table('users')
->where('votes', '<>', 100)
->get();
$users = DB::table('users')->where([
['status', '=', '1'],
['subscribed', '<>', '1'],
])->get();
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere('name', 'John')
->get();
$users = DB::table('users')
->whereBetween('votes', [1, 100])->get();
null
$users = DB::table('users')
->whereNull('updated_at')
->get();
$users = DB::table('users')
->whereNotNull('updated_at')
->get();
从一张表中获取一行/一列
$user = DB::table('users')->where('name', 'John')->first();
echo $user->name;//该方法将会返回单个 StdClass 对象:
$email = DB::table('users')->where('name', 'John')->value('email');
DB:raw
\DB::table(\DB::raw("xl_channel_goods as a"))
->whereIn(ChannelGood::DB_FIELD_CHANNEL_GOODS_CODE, $codes)
->get([
'a.*',
\DB::raw("(select goods_name from xl_goods as b where a.goods_sn=b.goods_sn) as goods_name"),
\DB::raw("(select group_concat(upc) from xl_goods_upc as c where a.goods_sn=c.goods_sn group by c.goods_sn) as upc")
]);
laravel CURD的更多相关文章
- php laravel curD
Laravel PHP Web开发框架 Laravel是一套简洁.优雅的PHP Web开发框架(PHP Web Framework).它可以让你从面条一样杂乱的代码中解脱出来:它可以帮你构建一个完美的 ...
- Laravel框架数据库CURD操作、连贯操作使用方法
Laravel框架数据库CURD操作.连贯如何来操作了这个操作性是非常的方便简单了我们在这里来为各位介绍一篇相关的教程,具体的细节步骤如下文介绍. Laravel是一套简洁.优雅的PHP Web开 ...
- 使用laravel一分钟搭建CURD后台页面
配置即一切 一切皆于需求,后台从0开始搭建,但是写了一两个页面后发现太多的是对单表的增删改查操作,于是就想到了,能不能做一个快速搭建的后台.想到一句话,配置即一切.如果一个CURD后台能只进行配置就自 ...
- Laravel框架数据库CURD操作、连贯操作
这篇文章主要介绍了Laravel框架数据库CURD操作.连贯操作.链式操作总结,本文包含大量数据库操作常用方法,需要的朋友可以参考下 一.Selects 检索表中的所有行 $users = DB::t ...
- Laravel框架数据库CURD操作、连贯操作总结
这篇文章主要介绍了Laravel框架数据库CURD操作.连贯操作.链式操作总结,本文包含大量数据库操作常用方法,需要的朋友可以参考下 一.Selects 检索表中的所有行 复制代码代码如下: $use ...
- 使用laravel搭建CURD后台页面
配置即一切 一切皆于需求,后台从0开始搭建,但是写了一两个页面后发现太多的是对单表的增删改查操作,于是就想到了,能不能做一个快速搭建的后台.想到一句话,配置即一切.如果一个CURD后台能只进行配置就自 ...
- Laravel框架中的数据库CURD操作、连贯操作、链式操作的用法
Laravel是一套简洁.优雅的PHP Web开发框架(PHP Web Framework).它可以让你从面条一样杂乱的代码中解脱出来:它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁.富于 ...
- 轻松搞定laravel的curd操作搞定简易留言版(四)
一:目的开发laravel简易留言板 二:路由操作routes.php <?php //GET /msg/index 展示留言列表 //GET /msg/add 展示表单 //POST /msg ...
- Laravel框架使用查询构造器实现CURD
一.什么是查询构造器? ①Laravel 查询构造器(query Builder)提供方便,流畅的接口,用来建立及执行数据库查找语法 ②使用PDO参数绑定,以保护应用程序免于SQL注入因此传入的参数不 ...
随机推荐
- PHP利用二叉堆实现TopK-算法的方法详解
前言 在以往工作或者面试的时候常会碰到一个问题,如何实现海量TopN,就是在一个非常大的结果集里面快速找到最大的前10或前100个数,同时要保证 内存和速度的效率,我们可能第一个想法就是利用排序,然后 ...
- 02-urllib库的get请求方式
对于urllib中的get请求方式,可以直接传入url的连接即可访问页面,但是对于要传入关键字的话,也可以用quote进行编码再传入. 案例如下: #get请求搜索参数如何添加 import urll ...
- 推荐一个好用的以多tab标签方式打开windows CMD的工具
最近我在做基于nodejs的微服务开发,需要在windows命令行里启动很多微服务.我的windows 10任务栏是这样子的: 我想找一款能像下图Chrome标签页这样打开windows 10 CMD ...
- 如何让邮件营销平台成为EDM神器?
任何一家做邮件营销的企业都希望自己的投入获得超乎想象的回报,出现打开率.点击率和伴随而来的成交量能够节节攀升的现象,这些数据我们当然可以通过监测各种平台的反馈而得到确切的报表.当然,作为邮件营销平台运 ...
- MySQL知识总结(二)基本语句总结
1. 数据库 查看数据库 show databases; 使用数据库 use [数据库名] 如:use mysql 创建数据库 CREATE DATABASE bruce DEFAULT CHARAC ...
- python第十二课——for in循环
1.for...in循环: 有两个使用场景: 场景一:for in和range对象配合使用 range对象的引入讲解 格式:range([start,end,step]): 特点:索引满足含头不含尾的 ...
- CSS3新特性2D、3D效果讲解
希望这篇博客可以对你有所帮助,如果有什么技术上的问题,希望我们可以做进一步的交流,如果你觉得我哪里阐述的不正确或者你有更好的更透彻的理解,也可以联系我,我在这里随时等着你. 对于css/html是每个 ...
- 【模板】deque实现单调队列
双端队列deque容器: 关于deque最常用的有这几个函数: 都是成员函数 双端队列模板题:[洛谷]P2952 [USACO09OPEN]牛线Cow Line #include<iostrea ...
- 搭建高可用mysql系列(1)-- Percona XtraDB Cluster介绍
Percona XtraDB Cluster (下文简称PXC)是一个开源的mysql 高可用解决方案.它将Percona Server和Percona XtraBackup与Galera库集成在一起 ...
- C++的四种显示类型转换
static_cast 除了含有底层const的类型转换,其他的一般都可以用这个static_cast const_cast 专门用来转换底层const,将常量转换为非常量,但是假如这个量如果本身是常 ...