1. 检索一个列值列表
    DB::table("tablename")->lists('mobile'); //5.3 及以上版本 lists 改为 pluck
  2. 返回
  3. [
  4. "13455556666",
  5. "13455556667",
  6. "13455556668",
  7. "13455556669",
  8. ]
  1. 指定一个自定义的键列返回的数组
  2. \DB::table('tablename')->lists('mobile','email');
  3. 返回
  4. [
  5. "aa@sina.com"=>"13022223335",
  6. "bb@sina.com"=>"13022223336",
  7. "cc@sina.com"=>"13022223337",
  8. ]
  1. 检索表中的所有行
  2.  
  3. $users = DB::table('users')->get();
  4. foreach ($users as $user)
  5. {
  6. var_dump($user->name);
  7. }
  1. 从表检索单个行
  2.  
  3. $user = DB::table('users')->where('name', 'John')->first();
  4. var_dump($user->name);
  1. 检索单行单列--返回指定字段
  2. DB::table('users')->where('name', 'John')->pluck('name');
    返回数组 非字符串
    ["Jone"] 并不是返回 "Jone"
  1. 指定一个Select子句
  2.  
  3. $users = DB::table('users')->select('name', 'email')->get();
  4. $users = DB::table('users')->distinct()->get();
  5. $users = DB::table('users')->select('name as user_name')->get();
  1. where
  2. $users = DB::table('users')->where('votes', '>', 100)->get();
  1. $users = DB::table('users')->where(['votes'=>100,'name'=>'zhangsan'])->get();
  1. OR
  2. $users = DB::table('users')->where('votes', '>', 100)->orWhere('name', 'John')->get();
  3.  
  4. Where Between
  5. $users = DB::table('users')->whereBetween('votes', array(1, 100))->get();
  6.  
  7. Where Not Between
  8. $users = DB::table('users')->whereNotBetween('votes', array(1, 100))->get();
  9.  
  10. Where In With An Array
  11.  
  12. $users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();
  13.  
  14. $users = DB::table('users')->whereNotIn('id', array(1, 2, 3))->get();
  15.  
  16. Using Where Null To Find Records With Unset Values
  17. $users = DB::table('users')->where('parent_id',1)->whereNull('updated_at')->get();
  18.  
  19. Order By, Group By, And Having
  20. $users = DB::table('users')->orderBy('name', 'desc')->groupBy('count')->having('count', '>', 100)->get();
  21.  
  22. Offset & Limit
  23. $users = DB::table('users')->skip(10)->take(5)->get();
  1. Joins
  2. DB::table('users')
  3.   ->join('contacts', 'users.id', '=', 'contacts.user_id')
  4.   ->join('orders', 'users.id', '=', 'orders.user_id')
  5.   ->select('users.id', 'contacts.phone', 'orders.price')
  6.   ->get();
  7.  
  8. DB::table('users')
  9.   ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
  10.   ->get();
  11.  
  12. DB::table('users')
  13.   ->join('contacts', function($join)
  14.   {
  15.   $join->on('users.id', '=', 'contacts.user_id')->orOn(...);
  16.   })
  17. ->get();
    //on 多个条件
  1. DB::table('users')
  2.   ->join('contacts', function($join)
  3.   {
  4.   $join->on('users.id', '=', 'contacts.user_id')->on(...);
  5.   })
  6. ->get();
  1. DB::table('users')   ->join('contacts', function($join)   {   $join->on('users.id', '=', 'contacts.user_id')   ->where('contacts.user_id', '>', 5);   })   ->get();
  1. 聚合
  2. $users = DB::table('users')->count();
  3. $price = DB::table('orders')->max('price');
  4. $price = DB::table('orders')->min('price');
  5. $price = DB::table('orders')->avg('price');
  6. $total = DB::table('users')->sum('votes');
  1. 递增或递减一个列的值
  2. DB::table('users')->increment('votes');
  3. DB::table('users')->increment('votes', 5);
  4. DB::table('users')->decrement('votes');
  5. DB::table('users')->decrement('votes', 5);
    指定额外的列更新
    DB::table('users')->increment('votes', 1, array('name' => 'John'));
  1. Inserts

  2. DB::table('users')->insert(
  3. array('email' => 'john@example.com', 'votes' => 0)
  4. );
  5.  
  6. $id = DB::table('users')->insertGetId(
  7. array('email' => 'john@example.com', 'votes' => 0)
  8. );
  9.  
  10. 多个记录插入到表中
  11.  
  12. DB::table('users')->insert(array(
  13. array('email' => 'taylor@example.com', 'votes' => 0),
  14. array('email' => 'dayle@example.com', 'votes' => 0),
  15. ));
  1. Updates
  2. DB::table('users')
  3. ->where('id', 1)
  4. ->update(array('votes' => 1));
  1. Deletes
  2. 删除表中的记录
  3.  
  4. DB::table('users')->where('votes', '<', 100)->delete();
  5.  
  6. 删除表中的所有记录
  7.  
  8. DB::table('users')->delete();
  9.  
  10. 清空一个表
  11.  
  12. DB::table('users')->truncate();
  1. 共享锁
  2. DB::table('users')->where('votes', '>',
  3. 100)->sharedLock()->get();
  4.  
  5. 更新“锁”
  6. DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();
  1. \DB::connection()->getPdo()->exec($sql)
  1. 事务 使用匿名函数 任何一个异常都会触发回滚
  2.  
  3. return \DB::transaction(function() use($user_params, $staff_params) {
  4. //写管理员进入user表
  5. $user_id = DB::table(User::TABLE_NAME)
  6. ->insertGetId($user_params);
  7. if(!$user_id) throw new Exception("写入user表失败");
  8.  
  9. $staff_params[Staff::DB_FIELD_USER_ID] = $user_id;
  10. $staff_id=DB::table(Staff::TABLE_NAME)
  11. ->insertGetId($staff_params);
  12. if(!$staff_id) throw new Exception("写入staff表失败");
  13.  
  14. return $staff_id;
  15. });
  1. 直接使用语句
  1. $ret = DB::select("select * from tablename where id=123"); //返回一个二维数组
    或者
  1. $ret = DB::select("select * from tablename where id=?",[123]); //返回一个二维数组
  1. 使用命名绑定来执行查询
  1. $results = DB::select('select * from users where id = :id', ['id' => 1]);
  1.  
  1. DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle']);
  1. $affected = DB::update('update users set votes = 100 where name = ?', ['John']);
  1.  
  1. $deleted = DB::delete('delete from users');
  1. DB::statement('drop table users');
  1.  
  1. //chunk()每次查n条
  2. $student=DB::table("vipinfo")->chunk(2,function($students){ //每次查2条
  3. var_dump($students);
  4. if(.......) return false; //在满足某个条件下使用return就不会再往下查了
  5. });
  1. 打印sql
    \DB::connection()->enableQueryLog();#开启log
  2. $aa = \DB::table(tablename)->first();
  3. $log = \DB::getQueryLog();
  4. dd($log);
  5. //打印
  6. array: [
  7. => array: [
  8. "query" => "select * from `fuli_xwc_merchant` limit 1"
  9. "bindings" => []
  10. "time" => 29.27
  11. ]
  12. ]
  1. 手动使用事务
  1.  
  2. 如果你想要手动开始事务从而对回滚和提交有一个完整的控制,可以使用 DB 门面的beginTransaction 方法:
  3.  
  4. DB::beginTransaction();
  5. 你可以通过 rollBack 方法回滚事务:
  6.  
  7. DB::rollBack();
  8. 最后,你可以通过 commit 方法提交事务:
  9.  
  10. DB::commit();
  11. 注意:使用 DB 门面的事务方法还可以用于控制查询构建器和 Eloquent ORM 的事务。

like

  1. $users = DB::table('users')
  2. ->where('name', 'like', 'T%')
  3. ->get();
  4. $users = DB::table('users')
  5. ->where('votes', '>=', 100)
  6. ->get();
  7.  
  8. $users = DB::table('users')
  9. ->where('votes', '<>', 100)
  10. ->get();
  11.  
  12. $users = DB::table('users')->where([
  13. ['status', '=', '1'],
  14. ['subscribed', '<>', '1'],
  15. ])->get();
  16. $users = DB::table('users')
  17. ->where('votes', '>', 100)
  18. ->orWhere('name', 'John')
  19. ->get();
  20. $users = DB::table('users')
  21. ->whereBetween('votes', [1, 100])->get();

  null

  1. $users = DB::table('users')
  2. ->whereNull('updated_at')
  3. ->get();
  4. $users = DB::table('users')
  5. ->whereNotNull('updated_at')
  6. ->get();

  从一张表中获取一行/一列

  1. $user = DB::table('users')->where('name', 'John')->first();
  2. echo $user->name;//该方法将会返回单个 StdClass 对象:
  3.  
  4. $email = DB::table('users')->where('name', 'John')->value('email');

DB:raw

  1. \DB::table(\DB::raw("xl_channel_goods as a"))
  2. ->whereIn(ChannelGood::DB_FIELD_CHANNEL_GOODS_CODE, $codes)
  3. ->get([
  4. 'a.*',
  5. \DB::raw("(select goods_name from xl_goods as b where a.goods_sn=b.goods_sn) as goods_name"),
  6. \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")
  7. ]);

  

laravel CURD的更多相关文章

  1. php laravel curD

    Laravel PHP Web开发框架 Laravel是一套简洁.优雅的PHP Web开发框架(PHP Web Framework).它可以让你从面条一样杂乱的代码中解脱出来:它可以帮你构建一个完美的 ...

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

    Laravel框架数据库CURD操作.连贯如何来操作了这个操作性是非常的方便简单了我们在这里来为各位介绍一篇相关的教程,具体的细节步骤如下文介绍.   Laravel是一套简洁.优雅的PHP Web开 ...

  3. 使用laravel一分钟搭建CURD后台页面

    配置即一切 一切皆于需求,后台从0开始搭建,但是写了一两个页面后发现太多的是对单表的增删改查操作,于是就想到了,能不能做一个快速搭建的后台.想到一句话,配置即一切.如果一个CURD后台能只进行配置就自 ...

  4. Laravel框架数据库CURD操作、连贯操作

    这篇文章主要介绍了Laravel框架数据库CURD操作.连贯操作.链式操作总结,本文包含大量数据库操作常用方法,需要的朋友可以参考下 一.Selects 检索表中的所有行 $users = DB::t ...

  5. Laravel框架数据库CURD操作、连贯操作总结

    这篇文章主要介绍了Laravel框架数据库CURD操作.连贯操作.链式操作总结,本文包含大量数据库操作常用方法,需要的朋友可以参考下 一.Selects 检索表中的所有行 复制代码代码如下: $use ...

  6. 使用laravel搭建CURD后台页面

    配置即一切 一切皆于需求,后台从0开始搭建,但是写了一两个页面后发现太多的是对单表的增删改查操作,于是就想到了,能不能做一个快速搭建的后台.想到一句话,配置即一切.如果一个CURD后台能只进行配置就自 ...

  7. Laravel框架中的数据库CURD操作、连贯操作、链式操作的用法

    Laravel是一套简洁.优雅的PHP Web开发框架(PHP Web Framework).它可以让你从面条一样杂乱的代码中解脱出来:它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁.富于 ...

  8. 轻松搞定laravel的curd操作搞定简易留言版(四)

    一:目的开发laravel简易留言板 二:路由操作routes.php <?php //GET /msg/index 展示留言列表 //GET /msg/add 展示表单 //POST /msg ...

  9. Laravel框架使用查询构造器实现CURD

    一.什么是查询构造器? ①Laravel 查询构造器(query Builder)提供方便,流畅的接口,用来建立及执行数据库查找语法 ②使用PDO参数绑定,以保护应用程序免于SQL注入因此传入的参数不 ...

随机推荐

  1. QtPropertyBrowser+vs2010的安装与配置

    之前编译过一次QtPropertyBrowser2.5,见文章http://www.cnblogs.com/aminxu/p/4516469.html,当时很激动,编译成功,lib也都编译通过,程序调 ...

  2. 安全紧急预警-防范新型 Sigrun 勒索病毒

    近日,互联网上出现一种 Sigrun 勒索病毒,其通过垃 圾邮件.网站捆绑软件等方式进行传播.该病毒一旦植入到 用户的服务器,将把系统文件加密为.sigrun 的文件,进而 向受害者勒索虚拟货币.该新 ...

  3. FTP工具FileZilla&WinSCP与FTP类库FluentFTP

    FileZilla Filezilla分为client和server.其中FileZilla Server是Windows平台下一个小巧的第三方FTP服务器软件,系统资源也占用非常小,可以让你快速简单 ...

  4. CentOS7 防火墙(firewall)的操作命令

    CentOS7 防火墙(firewall)的操作命令 安装:yum install firewalld 1.firewalld的基本使用 启动: systemctl start firewalld 查 ...

  5. php解析xml文件的方法

    最近一段时间在做模板包导入.模板包中包含有xml文件,,需要解析成给定的php数组格式. 我接触到了两种方法,分别是DOMDocument 方法和 simple_load_file. 个人偏好后一种, ...

  6. AOP的本质

    AOP的本质是HOOK: HOOK的本质是:新函数包含原函数或新函数替换原函数: 需要解决的问题: 1.新函数的生成: 2.新函数的调用机制: 3.原函数的调用机制: 新函数的生成: 1.将已有的动态 ...

  7. Hibernate三种状态;query查询;ResultTransformer转换为pojo对象;能够将query语句写在xml中;Criteria查询;ProjectionList总和/f分组等函数

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u010026901/article/details/24256091 Session操作过程中的po ...

  8. 【转】使用URL SCHEME启动天猫客户端并跳转到某个商品页面的方法

    在项目中遇到了这样一个需求:让用户在手机应用中,点击一个天猫的商品链接(知道商品在PC浏览器里的地址),直接启动天猫的客户端并显示这个商品.以前曾经实现过类似的功能,不过那次是淘宝的商品,天猫和淘宝的 ...

  9. docker 部署 redmine 项目管理软件

    最近部署一套redmine项目管理程序, ruby部署各种问题,用docker 直接run, 简单方便. . docker run --name=mysql-redmine -d -p : -v /d ...

  10. 第三部分 OpenStack镜像管理

    一.简介 很多源都有为OpenStack已经编译好的各种镜像了,您可以直接下载并通过使用这些镜像来熟悉OpenStack. 不过如果是为生产环境进行部署的话,您一定需要构建含有定制软件或配置的镜像文件 ...