A simple example of query builder for laravel framework

Time:2019-11-30

An example of this article describes the laravel framework query builder. To share with you for your reference, as follows:

public function query(){
  //New data
  //$bool = DB::table('wd_user')->insert(['username'=>'jack']);
  //dd($bool);
  //New data并且获取到自增id
  //$id = DB::table('wd_user')->insertGetid(['username'=>'Tom']);
  //dd($id);
  //Add multiple data
  //$bool = DB::table('wd_user')->insert([['username'=>'a'],['username'=>'d']]);
  //dd($bool);
  //Update data
  //$bool = DB::table('wd_user')->where('uid',7)->update(['username'=>'tom']);
  //dd($bool);
  // self increment
  //$bool = DB::table('wd_user')->increment('age',1);
  // self decreasing
  //$bool = DB::table('wd_user')->decrement('age',1);
  // self decreasing并且更新数据
  //$bool = DB::table('wd_user')->decrement('age',1,['name'=>'imooc']);
  //Delete data
  //$bool = DB::table('wd_user')->where('uid','>=',7)->delete();
  //dd($bool);
  // empty list
  //DB::table('wd_user')->truncate();
  //Get data
  //$user = DB::table('wd_user')->get();
  //dd($user);
  //Data sorting
  //$user = DB::table('wd_user')->orderBy('uid','desc')->get();
  //dd($user);
  //Add query criteria
  //$user = DB::table('wd_user')->where('uid','>=',5)->get();
  //dd($user);
  //Add multiple query criteria
  //$user = DB::table('wd_user')->where('uid > ? and age > ?',[5,18])->get();
  //dd($user);
  //Query specified fields
  //$user = DB::table('wd_user')->pluck('username');
  //dd($user);
  //Query the specified field with uid as the subscript
  //$user = DB::table('wd_user')->lists('username','uid');
  //dd($user);
  //Query some fields specified
  //$user = DB::table('wd_user')->select('uid','username')->get();
  //dd($user);
  //Segment query data
  //DB::table('wd_user')->chunk(2,function($user){
    //var_dump($user);
  //});
  //Count the number of records
  //$num = DB::table('wd_user')->count();
  //dd($num);
  //Query Max
  //$max = DB::table('wd_user')->max();
  //Query minimum
  //$min = DB::table('wd_user')->min();
  //Query average
  //$avg = DB::table('wd_user')->avg();
  //Statistics and values
  //$sum = DB::table('wd_user')->sum();
}

For more information about laravel, readers who are interested in the content can see the special topics of this site: introduction and advanced course of laravel framework, summary of PHP excellent development framework, introduction course of PHP object-oriented programming, introduction course of PHP + MySQL database operation and summary of PHP common database operation skills

I hope that this article will be helpful for you to Design PHP program based on laravel framework.