database additions and deletions to check curd operation
Add Data return bool
//$bool = Db::insert (' INSERT into student (Name,age) VALUES (?,?) ', [' Imooc ',]);
Var_dump ($bool);
Update data
//$num = db::update (' update student set age =? WHERE name =? ', [, ' Sean ']);
Var_dump ($num);
Query data
//$students = Db::select (' select * from student where ID >? ', [1001]);
DD ($students);
Delete data
$num = DB::d elete (' Delete from student where ID >? ', [1001]);
Var_dump ($num);
Database Operations-query BuilderIntroduction to Query Builder and new data use the Query Builder to modify data use the Query Builder to delete data use the Query Builder to query the aggregate functions in the data query Builder
1. Introduction to query BuilderThe Laravel Query Builder (Query Builder) provides a convenient, fluent interface for performing database lookup syntax using the PDO parameter bindings to protect the application from SQL injection so the incoming parameter does not need extra escape special characters can basically satisfy all the database operations, And can be performed on all supported database systems
1. Add data using Query Builder
Insert data return bool value inserted data
//$bool = db::table (' student ')->insert (
// [' Name ' => ' Imooc ', ' age ' =>18]
// );
Var_dump ($bool);
Insert data Return ID
//$id = db::table (' student ')->insertgetid (
// [' Name ' => ' Sean ', ' age ' =>18]
// );
Var_dump ($id);
Insert multiple data
$bool = db::table (' student ')->insert ([
' name ' => ' name1 ', ' age ' =>20],
[' Name ' => ' name2 ', ' age ' =>21],
]
;
Var_dump ($bool);
2. Update data with Query BuilderUpdate to the specified content self-increase and self-reduction
Updates data, returns the number of affected rows
//$num = db::table (' student ')
// ->where (' id ', 1003)
// ->update ([' Age ' = >30]);
Var_dump ($num);
Self-increasing default of 1 will all age from 1
//$num = db::table (' student ')->increment (' age ');
Self-amplification adds all age to 3
//$num = db::table (' student ')->increment (' Age ', 3);
Self-reduction will all age self minus 3
//$num = db::table (' student ')->decrement (' Age ', 3);
Self-reduction default 1 will all age self minus 1
//$num = db::table (' student ')->decrement (' age ');
Reduce the age of 1003 by 3
//$num = db::table (' student ')
// ->where (' id ', 1003)
// -> Decrement (' age ', 3);
At the same time, modify other fields
$num = db::table (' student ')
->where (' id ', 1003)
->decrement (' age ', 3,[' name ' = > ' IIMOOC ']);
Var_dump ($num);
3. Delete data using the Query BuilderDelete truncate
$num = db::table (' student ')
// ->where (' id ', 1006)
// ->delete ();
$num = db::table (' student ')
// ->where (' id ', ' >= ', 1003)
// ->delete ();
Var_dump ($num);
Do not return anything delete all data
db::table (' Student ')->truncate ();
4. Query data using Query BuilderGet () A () a Where () pluck () lists () Select () chunk ()
Insert data returns the bool value of inserting data//$bool = db::table (' student ')->insert ([//[' ID ' =>1001, ' name ' => ' name1 ', ' age ' =>18], [' ID ' =>1002, ' name ' => ' name2 ', ' age ' =>18],//[' ID ' =>1003, ' name ' => ' Name3 ', ' age ' =>19],//
[' ID ' =>1004, ' name ' => ' name4 ', ' age ' =>20],//[' ID ' =>1005, ' name ' => ' name5 ', ' age ' =>21]//];
Var_dump ($bool);
Get () gets all the data//$students = db::table (' student ') of the Table->get ();
First () Gets the number one data//$student = db::table (' student ')//->orderby (' id ', ' desc ')//->first ();
where//$students = db::table (' student ')//->where (' id ', ' >= ', 1002)//->get ();
Multiple conditions//$students = db::table (' student ')//->whereraw (' ID >=? and age >? ', [1001,18])//->get ();
Pluck returns the field in the result set//$names = db::table (' student ')//->pluck (' name ');
Lists returns the field in the result set specifies a field to do subscript//$names = db::table (' student ')//->lists (' name ', ' id '); Select//$selects = db::table (' student ')//->select (' ID', ' name ', ' age ')//->get ();
DD ($selects);
Chunk echo ' <pre> ';
Db::table (' Student ')->chunk (2,function ($students) {var_dump ($students);
if (true) {return false; }
});
5. Using aggregate functions in the query builderCount () Max () min () Avg () sum ()
$num = db::table (' student ')->count ();
$max = db::table (' student ')->max (' age ');
$min = db::table (' student ')->min (' age ');
$avg = db::table (' student ')->avg (' age ');
$sum = db::table (' student ')->sum (' age ');
Var_dump ($sum);