关键词搜索

源码搜索 ×
×

PHP笔记-laravel框架中的数据库查询构造器

发布2022-05-09浏览954次

详情内容

程序运行截图如下:

对应的表结构是这样的:

  1. /*
  2. SQLyog Ultimate v12.09 (64 bit)
  3. MySQL - 5.7.12 : Database - laravel
  4. *********************************************************************
  5. */
  6. /*!40101 SET NAMES utf8 */;
  7. /*!40101 SET SQL_MODE=''*/;
  8. /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
  9. /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
  10. /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
  11. /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
  12. CREATE DATABASE /*!32312 IF NOT EXISTS*/`laravel` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */;
  13. USE `laravel`;
  14. /*Table structure for table `student` */
  15. DROP TABLE IF EXISTS `student`;
  16. CREATE TABLE `student` (
  17. `id` int(11) NOT NULL AUTO_INCREMENT,
  18. `name` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  19. `age` int(11) DEFAULT NULL,
  20. PRIMARY KEY (`id`)
  21. ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  22. /*Data for the table `student` */
  23. insert into `student`(`id`,`name`,`age`) values (2,'小张',19),(3,'小明',18),(4,'小张',19);
  24. /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
  25. /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
  26. /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
  27. /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

源码如下:

路由web.php添加:

  1. Route::prefix('student')->group(function(){
  2. ......
  3. ......
  4. Route::get('qbInsert', 'StudentController@qbInsert');
  5. Route::get('qbSelect', 'StudentController@qbSelect');
  6. Route::get('qbModify', 'StudentController@qbModify');
  7. Route::get('qbDelete', 'StudentController@qbDelete');
  8. });

这个config/database.php是数据库相关的设置,目前不用改,对应mysql是这样的。

需要修改的是

.env

修改为如下:

  1. DB_CONNECTION=mysql
  2. DB_HOST=127.0.0.1
  3. DB_PORT=3306
  4. DB_DATABASE=laravel
  5. DB_USERNAME=root
  6. DB_PASSWORD=root

新建StudentController.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\DB;
  4. class StudentController extends Controller{
  5. ......
  6. ......
  7. ......
  8. ......
  9. public function qbSelect(){
  10. //get是获取所有数据
  11. $students = DB::table('student')->get();
  12. dd($students);
  13. //first查询第一条数据
  14. $student = DB::table('student')->first();
  15. dd($student);
  16. //first + 排序
  17. $student = DB::table('student')
  18. ->orderBy('id', 'desc')
  19. ->first();
  20. dd($student);
  21. //加where
  22. $students = DB::table('student')
  23. ->whereRaw('id >= ? and age > ?', [5, 16])
  24. ->get();
  25. dd($student);
  26. //pluck返回结果集中指定字段
  27. $names = DB::table('student')->pluck('name');
  28. dd($names);
  29. //lists一样的效果,但可以指定某个键,作为下标
  30. $names = DB::table('student')->lists('name', 'id');
  31. dd($names);
  32. //select指定查询的字段
  33. $students = DB::table('student')->select('id', 'name', 'age')->get();
  34. dd($students);
  35. //chunk:每次查指定数据,比如2条,防止一次性查出,内存爆了,在某个地方停止,就return false
  36. echo '<pre>';
  37. DB::table('student')->chunk(2, function($students){
  38. var_dump($students);
  39. //条件,停止就返回false
  40. //return false;
  41. });
  42. //聚合函数
  43. var_dump(DB::table('student')->count());
  44. var_dump(DB::table('student')->max('age'));
  45. var_dump(DB::table('student')->min('age'));
  46. var_dump(DB::table('student')->avg('age'));
  47. var_dump(DB::table('student')->sum('age'));
  48. return "qbSelect";
  49. }
  50. public function qbDelete(){
  51. $num = DB::table('student')
  52. ->where('id', 2)
  53. ->delete();
  54. var_dump($num);
  55. $num = DB::table('student')
  56. ->where('id', '>=', 4)
  57. ->delete();
  58. var_dump($num);
  59. //删除所有
  60. DB::table('student')->truncate();
  61. return "qbDelete";
  62. }
  63. public function qbModify(){
  64. $num = DB::table('student')
  65. ->where('id', 2)
  66. ->update(['age' => 50]);
  67. DB::table('student')->increment('age', 2);
  68. DB::table('student')->decrement('age', 3);
  69. var_dump($num);
  70. //同时修改
  71. $num = DB::table('student')
  72. ->where('id', 12)
  73. ->decrement('age', 2, ['name' => '呵呵']);
  74. var_dump($num);
  75. return "qbModify";
  76. }
  77. public function qbInsert(){
  78. $bool = DB::table('student')->insert(
  79. ['name' => '小白', 'age' => 16]
  80. );
  81. var_dump($bool);
  82. $id = DB::table('student')->insertGetId(
  83. ['name' => '小黄', 'age' => 15]
  84. );
  85. var_dump($id);
  86. $bool = DB::table('student')->insert([
  87. ['name' => 'name1', 'age' => 30],
  88. ['name' => 'name2', 'age' => 31]
  89. ]);
  90. var_dump($bool);
  91. return "qbInsert";
  92. }
  93. }

 

 

 

 

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载