Simple Example of Laravel Framework Query Constructor
- 2021-12-09 08:33:36
- OfStack
This article illustrates the Laravel Framework Query Constructor. Share it for your reference, as follows:
public function query(){
// Add data
//$bool = DB::table('wd_user')->insert(['username'=>'jack']);
//dd($bool);
// Add data and get self-increment id
//$id = DB::table('wd_user')->insertGetid(['username'=>'Tom']);
//dd($id);
// Add multiple pieces of 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-increasing
//$bool = DB::table('wd_user')->increment('age',1);
// Self-reduction
//$bool = DB::table('wd_user')->decrement('age',1);
// Self-subtract and update data
//$bool = DB::table('wd_user')->decrement('age',1,['name'=>'imooc']);
// Delete data
//$bool = DB::table('wd_user')->where('uid','>=',7)->delete();
//dd($bool);
// Empty a table
//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 the specified field
//$user = DB::table('wd_user')->pluck('username');
//dd($user);
// Queries the specified field and uses the uid As a subscript
//$user = DB::table('wd_user')->lists('username','uid');
//dd($user);
// Queries the specified 1 Some fields
//$user = DB::table('wd_user')->select('uid','username')->get();
//dd($user);
// Segmented query data
//DB::table('wd_user')->chunk(2,function($user){
//var_dump($user);
//});
// Number of statistical records
//$num = DB::table('wd_user')->count();
//dd($num);
// Query maximum value
//$max = DB::table('wd_user')->max();
// Query minimum value
//$min = DB::table('wd_user')->min();
// Average number of queries
//$avg = DB::table('wd_user')->avg();
// Statistical sum value
//$sum = DB::table('wd_user')->sum();
}
More readers interested in Laravel can check the topics of this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"
Hope that this article is based on the framework of Laravel PHP programming help.