[Laravel框架学习二]:Laravel的CURD和查询构造器的CURD,以及聚合函数

  1 public function index()
  2     {
  3 
  4         //return Member::getMember();//这是调用模型的方法
  5         
  6         return view('lpc',[
  7             'age'=>18,
  8             'name'=>'PengchongLee',
  9             ]);
 10     }
 11     public function test()//一个方法得设置一个路由
 12     {
 13         //echo 1;//测试路由
 14         
 15         //新增数据
 16         //$data = DB::insert('insert into test(name,age) values(?,?) ',['lisi','18']);
 17         //var_dump($data);
 18         
 19         //删除数据
 20         //$data = DB::delete('delete from test where id= ?',['1']);
 21         //var_dump($data);//返回的是响应条数
 22         
 23         //更新数据
 24         //$data = DB::update('update test set name=? where id=?',['zhangsan','2']);
 25         //var_dump($data);
 26         
 27         //查询数据
 28         $data = DB::select('select * from test');
 29         DD($data);//DD 是laravel的一种打印方式,比较复杂
 30     }
 31 
 32     //laravel的模式查询构造器
 33     public function test1()
 34     {
 35         
 36         //使用查询构造器添加数据
 37         
 38         // $res = DB::table('test')->insert([
 39         //     'name' => 'doubi',
 40         //     'age' => '18s',
 41         //     ]);//添加单条数据
 42 
 43         // $res = DB::table('test')->insert([
 44         //     ['name'=>'a','age'=>'33'],
 45         //     ['name'=>'b','age'=>'332'],
 46         //     ]);//添加多条数据
 47         $res = DB::table('test')->insertGetId(
 48             ['name'=>'zhangsan','age'=>18]
 49             );//获取添加数据ID
 50         echo $res;
 51 
 52         //使用查询构造器更新数据
 53         
 54         // $res = DB::table('test')
 55         //     ->where('id',7)
 56         //     ->update(['age'=>332]);
 57         
 58         //$res = DB::table('test')->increment('age');//这是自增,每条自增加一
 59         //$res = DB::table('test')->decrement('age');//这是自减,每条自增加一
 60         //$res = DB::table('test')->increment('age',3);//这是自增加3
 61         //$res = DB::table('test')->decrement('age',3);//这是自减减3
 62         // $res = DB::table('test')
 63         //     ->where('id',7)
 64         //     //->decrement('age');//加条件的自减减1
 65         //     ->decrement('age',3,['name'=>'doubi']);//加条件改自增字段,顺变改其他字段
 66         // var_dump($res);    
 67     }
 68     public function test2()
 69     {
 70         //使用查询构造器删除数据
 71         
 72         // $res = DB::table('test')
 73         //     ->where('id',4)
 74         //     ->delete();//删除单条
 75         
 76         // $res = DB::table('test')
 77         //     ->where('id','>=',9)
 78         //     ->delete();
 79         
 80         DB::table('test')->truncate();//清空表,一般不使用ID从1开始
 81         
 82     }
 83     public function test3()
 84     {
 85         //使用查询构造器查询数据
 86         
 87         //get获取所有数据
 88         //$res = DB::table('test')->get();
 89 
 90         //first查的是一条数据
 91         // $res = DB::table('test')
 92         //     ->orderby('id','desc')
 93         //     ->first();
 94         
 95         //where
 96         // $res = DB::table('test')
 97         //     //->where('id','>=','2')
 98         //     ->whereRaw('id >=? and age >?',[4,20])//注意是whereRaw
 99         //     ->get();
100 
101         //pluck返回想要字段
102         // $res = DB::table('test')
103         //         ->pluck('age');
104 
105         //lists可以指定键值
106         // $res = DB::table('test')
107         //         ->lists('age','id');//可以指定键值id是键值
108 
109         //select指定查找字段
110         // $res = DB::table('test')
111         //         ->select('id','name')
112         //         ->get();
113 
114         //chunk可以设置每次查几条,true查完为止
115         echo "<pre>";
116         $res = DB::table('test')->chunk(2,function($res){
117             var_dump($res);
118             return false;//设置后只查一个=次两条数据
119         });    
120     }
121     public function test4()
122     {
123         //查询构造器的聚合函数
124         
125         //$res = DB::table('test')->count();//查询数据总条数
126         //$res = DB::table('test')->max('age');//最大值
127         //$res = DB::table('test')->min('age');//最小值
128         //$res = DB::table('test')->sum('age');//求和
129         $res = DB::table('test')->avg('age');//平均值
130         var_dump($res);
131     }

视图层

    <center>
        <h1>试一下视图层</h1>
        <h2>{{ Inspiring::quote() }}</h2>
        <h2>简单是终极的世故。-达·芬奇</h2>

        <h3>{{$name}}</h3><!-- 渲染的时候传的值,这样可以输出出来 -->
        <h3>{{$age}}</h3>
    </center>

 传一下我的路由文件routes.php

 1 Route::get('base', function(){
 2     return 'Hello word!';
 3 });//这种传输一般不用url传值的!
 4 
 5 Route::post('base1', function(){
 6     return 'base1';
 7 });//post方式不能url传值
 8 
 9 
10 Route::get('lpc', 'LpcController@index');//get和post是基础的传输方式
11 Route::get('test', 'LpcController@test');
12 Route::get('test1', 'LpcController@test1');
13 Route::get('test2', 'LpcController@test2');
14 Route::get('test3', 'LpcController@test3');
15 Route::get('test4', 'LpcController@test4');
16 
17 Route::match(['get','post'],'lpc', 'LpcController@index');//match可以指定传输方式
18 
19 //Route::any('lpc', 'LpcController@index');//any各种传输方式都可以
原文地址:https://www.cnblogs.com/lipcblog/p/6653558.html