博客列表 >laravel7 高级数据库操作&&Model类

laravel7 高级数据库操作&&Model类

赵大叔
赵大叔原创
2020年06月05日 15:44:05891浏览

数据库操作

laravel 操作数据库的一些方法

  • first(): 查询单条
  • get(): 查询多条
  • all(): 输出格式转换,数组
  • select(): 选择要查询字段
  • as: 取别名
  • where(): 条件查询, 允许有多个(and)
  • orWhere(): 条件查询, 允许有多个(or)
  • like 模糊查询
  • tosql(): 查看生 sql 语句
  • where in(): 多条件
  • join(): 连表查询
  • avg('pc'): 平均
  • sum('pc'): 求和
  • min('pc'): 最小
  • max('pc'): 最大
  • count(): 表记录数
  • insert(): 参数是 array,支持二维
  • insertGetId: 插入并返回 id
  • update(): 更新, 参数是 array
  • delete(): 删除

aravel 操作数据库方法演示:

  1. // 数据库查询(链式)
  2. // first() 单条记录
  3. public function finds(){
  4. echo '<pre>';
  5. // first() 单条记录
  6. $res = DB::table('article')->where('id', 2)->first();
  7. print_r($res);
  8. }
  9. // 数据库查询(链式)
  10. // 查询多条 get()
  11. // all() : 输出格式转换
  12. // select(): 选择要查询字段
  13. // as: 取别名
  14. // where(): 条件查询, 允许有多个(and)
  15. // orWhere(): 条件查询, 允许有多个(or)
  16. public function list(){
  17. echo '<pre>';
  18. $res = DB::table('article')->select('cate_id as cid', 'title as 标题')->
  19. where('cate_id', '=', 2)->get()->all();
  20. print_r($res);
  21. }
  22. // 数据库查询(链式)
  23. // 查询多条 get()
  24. // like 查询
  25. // where():
  26. // tosql (): 查看生sql语句
  27. public function likes(){
  28. echo '<pre>';
  29. // $res = DB::table('article')->where('cate_id', '=', '2')
  30. // ->where('id', '>', '2')->get()->all();
  31. $res = DB::table('article')->where('cate_id', '=', '2')
  32. ->orWhere('cate_id', '=', '8')->tosql();//->get()->all();
  33. print_r($res);
  34. }
  35. // 数据库查询(链式)
  36. // where in()
  37. public function whereIn(){
  38. echo '<pre>';
  39. $res = DB::table('article')->whereIn('id', [1,2,5])->get()->all();
  40. print_r($res);
  41. }
  42. // 数据库查询(链式)
  43. // join() 连表查询
  44. public function joins(){
  45. echo '<pre>';
  46. $res = DB::table('article')->join('users', 'users.id', '=', 'article.uid')
  47. ->select('article.title', 'users.name')->get()->all();
  48. print_r($res);
  49. }
  50. // 数据库查询(链式)
  51. // 聚合函数
  52. public function pvs(){
  53. echo '<pre>';
  54. // $res = DB::table('article')->avg('pc');
  55. // $res = DB::table('article')->sum('pc');
  56. // $res = DB::table('article')->min('pc');
  57. // $res = DB::table('article')->max('pc');
  58. $res = DB::table('article')->count();
  59. print_r($res);
  60. }
  61. // 数据库增加(链式)
  62. // insert():参数是array,支持二维
  63. public function insert2(){
  64. $item = ['uid'=>2, 'cate_id'=>6, 'title'=>'如何解决php错误不提示 ,空白页面的问题', 'pc'=>200];
  65. $item1 = ['uid'=>2, 'cate_id'=>1, 'title'=>'PHP 转 Go 还是转 Java', 'pc'=>600];
  66. $item2 = ['uid'=>2, 'cate_id'=>2, 'title'=>'如何去除word自动生成目录中的空格', 'pc'=>1200];
  67. $data[] = $item1;
  68. $data[] = $item2;
  69. $res = DB::table('article')->insert($data);
  70. var_dump($res);
  71. }
  72. // insertGetId, 插入并返回id
  73. public function insert3(){
  74. $item = ['uid'=>2, 'cate_id'=>6, 'title'=>'空白页面的问题', 'pc'=>200];
  75. $res = DB::table('article')->insertGetId($item);
  76. var_dump($res);
  77. }
  78. // 数据库更新(链式)
  79. // pdate() 参数是array
  80. // whereIn() 多条
  81. public function update1(){
  82. $res = DB::table('article')->where('id','=', 8)->update(array('title'=>'update测试'));
  83. var_dump($res);
  84. }
  85. // 数据库删除(链式)
  86. // delete() 参数是array
  87. // whereIn() 多条
  88. public function delete1(){
  89. $res = DB::table('article')->where('id','=', 8)->delete();
  90. var_dump($res);
  91. }

Model类

  • laravel:没有单独创建Model类,但是支持,倾向于把Model类作为与数据库打交道的类。

使用命令创建一个Model类

  • 命令:php artisan make:model Article
  • 使用Model类可以不用DB::table
  1. // Model类
  2. <?php
  3. namespace App;
  4. use Illuminate\Database\Eloquent\Model;
  5. class Article extends Model
  6. {
  7. // 设置属性指定查询表名
  8. // protected $table = 'article';
  9. protected $table = 'users';
  10. }
  11. // 使用模型类
  12. // 模型
  13. public function mymodel(Article $article){
  14. $res = $article->get()->toArray();
  15. echo '<pre>';
  16. print_r($res);
  17. }

总结

项目开发中,大部分功能都要和数据库打交道,学好数据库操作很重要。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议