Laravel--查詢構造器 方法 及聚合函數


controller內內容如下

<?php

/**

 * Created by PhpStorm.

 * User: work

 * Date: 2018/1/2

 * Time: 20:36

 */

namespace App\Http\Controllers;

 

use App\Student\Student;

use Illuminate\Support\Facades\DB;

Route::any('query1','StudentController@query1');查詢構造器方法  添加方法

  public function query1()

    {

//  返回布爾     單個添加$bloon = DB::table('student')->insert(['name'=>'gwz','age'=>18]);

//  返回當前添加行   $num = DB::table('student')->insertGetId(['name'=>'ggg','age'=>3]);

//   批量添加返回布爾  $bloon =    DB::table('student')->insert([

//            ['name'=>'gwz1','age'=>11],

//            ['name'=>'gwz22','age'=>111],

//        ]);

    }

Route::any('query2','StudentController@query2');查詢構造器方法  修改方法

    //查詢構造器方法  修改方法

    public function query2()

    {

//      影響的行數  $num =  DB::table('student')->where(['id'=>5])->update(['age'=>0]);

 

//        修改年齡增加

//   受影響行數  默認修改年齡數自增為 age=age+1  $num = DB::table('student')->increment('age');

//   受影響行數  默認修改年齡數自增為 age=age+3  $num = DB::table('student')->increment('age',3);

 

       //修改年齡減小

//       受影響行數 默認修改年齡為age=age-1  $num = DB::table('student')->decrement('age');

//       受影響行數 默認修改年齡為age=age-3  $num = DB::table('student')->decrement('age',3);

 

 

//受影響行數  id為5 年齡 age=age+3 同時修改`name`為hahahah  $num = DB::table('student')->where(['id'=>5])->increment('age',3,['name'=>'hahahah']);

//        var_dump($num);

    }

Route::any('query3','StudentController@query3');查詢構造器方法 刪除方法

    //查詢構造器方法 刪除方法

    public function query3()

    {

//     返回受影響行數  刪除條件為 id = 5  $num = DB::table('student')->where('id','5')->delete();

//     返回受影響行數  刪除條件為 id >= 5  $num = DB::table('student')->where('id','>=','5')->delete();

//清空表 truncate 非常危險 不返回任何        DB::table('student')->truncate();

    }

 

Route::any('query4','StudentController@query4');查詢構造器  查詢方法

    //查詢構造器  查詢方法

    public function query4()

    {

//        主要方法有  get() first() where() pluck()  select()

//        獲取數據get()

//   獲取所有數據    $student = DB::table('student')->get()->toArray();

//        沒什么卵用first()

//   獲取倒序ID的第一條ID    $student = DB::table('student')->orderBy('id','desc')->first();

//

//

//       條件查詢where()

//     獲取ID大於0的所有數據 並且變為數組   $student = DB::table('student')->where('id','>=',0)->get()->toArray();

//        復合條件查詢whereRaw()

//    獲取id是11並且年齡是11 並且變為數組    $student = DB::table('student')->whereRaw("id=? and age =?",['11','11'])->get()->toArray();

 

        //pluck()中文意思為摘下的意思  摘下一個字段存為一個數組

//    將字段`name`摘下存入一個數組    $names = DB::table('student')->pluck('name')->toArray();

       //pluck()

//    將字段`name`摘下來  數組形式 為 array('id'=>'name')   $names = DB::table('student')->pluck('name','id')->toArray();

       //select()指定要查詢的字段

//     查詢`age`,`name`字段內容 且變為二維數組   $student = DB::table('student')->select('age','name')->get()->toArray();

 

        //chunk()用來處理大量數據

//        5.4的不會用暫時不用

// 每次查1000條為一個數組 滿足條件則終止查詢

//        DB::table('student')->chunk(1000,function ($msg){

//            var_dump($msg);

//            if(你的條件)

//            {

//                return false;

//            }

//        });

    }

Route::any('query5','StudentController@query5');聚合函數

    //聚合函數

    public function query5()

    {

        //統計條數count()

//       表中所有條數 $num = DB::table('student')->count();

        //max()取最大

        //統計字段中數值最大的 $num = DB::table('student')->max('age');

        //min()取最小

        //統計字段中數值最小的值 $num = DB::table('student')->min('age');

        //avg()

        //統計字段中數值平均值 $num = DB::table('student')->avg('age');

        //sum()

        //統計字段中數值的和 

$request->all()//獲取所有參數if($request->isMethod('GET')){判斷是否是GET請求}$res = $request->is('student/*');$res = $request->ajax();========================================================
laravel 使用 session 路由配置Route::group(['middleware'=>['web']],function (){    Route::any('session1',['uses'=>'StudentController@session1']);    Route::any('session2',['uses'=>'StudentController@session2']);});
StudentController 內 存入sessionSession存入數組內方法Sssion::push('key','value1');Sssion::push('key','value2');取出Session中的數組並且刪除Session::pull('key');
判斷Session中某個Key在不在Session::has('key')public function session1(Request $request)    {        //HTTP request session();        $request->session()->put('key1','val1');Session::put('key','value');    }
StudentController 內 讀取 session  public function session2(Request $request)    {        $session = $request->session()->get('key1');Session::get('key','如果key不存在可以在這里寫默認值');        var_dump($session);    }
php變量轉json public function response()    {        $data = ['errCode'=>0,                'errMsg'=>'success',            'data'=>'dada'            ];        return response()->json($data);    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM