laravel DB门面操作

2018-07-10 03:54:35   php分享记录

 

DB门面操作

  1. 基本使用
  2. DB::connection('connection_name');
  3. // 运行数据库查询语句
  4. $results = DB::select('select * from users where id = ?', [1]);
  5. $results = DB::select('select * from users where id = :id', ['id' => 1]);
  6. // 运行普通语句
  7. DB::statement('drop table users');
  8. // 监听查询事件
  9. DB::listen(function($sql, $bindings, $time){ code_here; });
  10. // 数据库事务处理
  11. DB::transaction(function()
  12. {
  13. DB::table('users')->update(['votes' => 1]);
  14. DB::table('posts')->delete();
  15. });
  16. DB::beginTransaction();
  17. DB::rollBack();
  18. DB::commit();
  19. 查询语句构造器
  20. // 取得数据表的所有行
  21. DB::table('name')->get();
  22. // 取数据表的部分数据
  23. DB::table('users')->chunk(100, function($users)
  24. {
  25. foreach ($users as $user)
  26. {
  27. //
  28. }
  29. });
  30. // 取回数据表的第一条数据
  31. $user = DB::table('users')->where('name', 'John')->first();
  32. DB::table('name')->first();
  33. // 从单行中取出单列数据
  34. $name = DB::table('users')->where('name', 'John')->pluck('name');
  35. DB::table('name')->pluck('column');
  36. // 取多行数据的「列数据」数组
  37. $roles = DB::table('roles')->lists('title');
  38. $roles = DB::table('roles')->lists('title', 'name');
  39. // 指定一个选择字句
  40. $users = DB::table('users')->select('name', 'email')->get();
  41. $users = DB::table('users')->distinct()->get();
  42. $users = DB::table('users')->select('name as user_name')->get();
  43. // 添加一个选择字句到一个已存在的查询语句中
  44. $query = DB::table('users')->select('name');
  45. $users = $query->addSelect('age')->get();
  46. // 使用 Where 运算符
  47. $users = DB::table('users')->where('votes', '>', 100)->get();
  48. $users = DB::table('users')
  49. ->where('votes', '>', 100)
  50. ->orWhere('name', 'John')
  51. ->get();
  52. $users = DB::table('users')
  53. ->whereBetween('votes', [1, 100])->get();
  54. $users = DB::table('users')
  55. ->whereNotBetween('votes', [1, 100])->get();
  56. $users = DB::table('users')
  57. ->whereIn('id', [1, 2, 3])->get();
  58. $users = DB::table('users')
  59. ->whereNotIn('id', [1, 2, 3])->get();
  60. $users = DB::table('users')
  61. ->whereNull('updated_at')->get();
  62. DB::table('name')->whereNotNull('column')->get();
  63. // 动态的 Where 字句
  64. $admin = DB::table('users')->whereId(1)->first();
  65. $john = DB::table('users')
  66. ->whereIdAndEmail(2, 'john@doe.com')
  67. ->first();
  68. $jane = DB::table('users')
  69. ->whereNameOrAge('Jane', 22)
  70. ->first();
  71. // Order By, Group By, 和 Having
  72. $users = DB::table('users')
  73. ->orderBy('name', 'desc')
  74. ->groupBy('count')
  75. ->having('count', '>', 100)
  76. ->get();
  77. DB::table('name')->orderBy('column')->get();
  78. DB::table('name')->orderBy('column','desc')->get();
  79. DB::table('name')->having('count', '>', 100)->get();
  80. // 偏移 & 限制
  81. $users = DB::table('users')->skip(10)->take(5)->get();
  82. Joins
  83. // 基本的 Join 声明语句
  84. DB::table('users')
  85. ->join('contacts', 'users.id', '=', 'contacts.user_id')
  86. ->join('orders', 'users.id', '=', 'orders.user_id')
  87. ->select('users.id', 'contacts.phone', 'orders.price')
  88. ->get();
  89. // Left Join 声明语句
  90. DB::table('users')
  91. ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
  92. ->get();
  93. // select * from users where name = 'John' or (votes > 100 and title <> 'Admin')
  94. DB::table('users')
  95. ->where('name', '=', 'John')
  96. ->orWhere(function($query)
  97. {
  98. $query->where('votes', '>', 100)
  99. ->where('title', '<>', 'Admin');
  100. })
  101. ->get();
  102. 聚合
  103. $users = DB::table('users')->count();
  104. $price = DB::table('orders')->max('price');
  105. $price = DB::table('orders')->min('price');
  106. $price = DB::table('orders')->avg('price');
  107. $total = DB::table('users')->sum('votes');
  108. 原始表达句
  109. $users = DB::table('users')
  110. ->select(DB::raw('count(*) as user_count, status'))
  111. ->where('status', '<>', 1)
  112. ->groupBy('status')
  113. ->get();
  114. // 返回行
  115. DB::select('select * from users where id = ?', array('value'));
  116. DB::insert('insert into foo set bar=2');
  117. DB::update('update foo set bar=2');
  118. DB::delete('delete from bar');
  119. // 返回 void
  120. DB::statement('update foo set bar=2');
  121. // 在声明语句中加入原始的表达式
  122. DB::table('name')->select(DB::raw('count(*) as count, column2'))->get();
  123. Inserts / Updates / Deletes / Unions / Pessimistic Locking
  124. // 插入
  125. DB::table('users')->insert(
  126. ['email' => 'john@example.com', 'votes' => 0]
  127. );
  128. $id = DB::table('users')->insertGetId(
  129. ['email' => 'john@example.com', 'votes' => 0]
  130. );
  131. DB::table('users')->insert([
  132. ['email' => 'taylor@example.com', 'votes' => 0],
  133. ['email' => 'dayle@example.com', 'votes' => 0]
  134. ]);
  135. // 更新
  136. DB::table('users')
  137. ->where('id', 1)
  138. ->update(['votes' => 1]);
  139. DB::table('users')->increment('votes');
  140. DB::table('users')->increment('votes', 5);
  141. DB::table('users')->decrement('votes');
  142. DB::table('users')->decrement('votes', 5);
  143. DB::table('users')->increment('votes', 1, ['name' => 'John']);
  144. // 删除
  145. DB::table('users')->where('votes', '<', 100)->delete();
  146. DB::table('users')->delete();
  147. DB::table('users')->truncate();
  148. // 集合
  149. // unionAll() 方法也是可供使用的,调用方式与 union 相似
  150. $first = DB::table('users')->whereNull('first_name');
  151. $users = DB::table('users')->whereNull('last_name')->union($first)->get();
  152. // 消极锁
  153. DB::table('users')->where('votes', '>', 100)->sharedLock()->get();
  154. DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();