Laravel Framework Query Constructor CURD Operation Sample
- 2021-12-21 04:29:43
- OfStack
This article illustrates the CURD operation of the Laravel Framework Query Constructor. Share it for your reference, as follows:
Add
// Insert 1 Bar data
public function insert(){
$rs = DB::table('student')->insert([
'name' => 'Kit',
'age' => 12
]);
dd($rs); //true
}
// Insert 1 Bar data and return self-increment ID
public function insert(){
$id = DB::table('student')->insertGetId([
'name'=>'Tom',
'age'=>11
]);
dd($id); //1004
}
// Insert multiple pieces of data
public function insert(){
$rs = DB::table('student')->insert([
['name'=>'Ben','age'=>22],
['name'=>'Jean','age'=>23]
]);
dd($rs);//true
}
Update
// Update 1 Bar data
public function update(){
$rs = DB::table('student')
->where('id',1003)
->update(['age'=>10]);
dd($rs);//1 Returns the number of rows affected
}
// Self-updating
public function update(){
// All ages plus 1
$rs = DB::table('student')->increment('age');
dd($rs);//5 Returns the number of rows affected
//ID For 1001 Age of plus 3
$rs = DB::table('student')
->where('id',1001)
->increment('age',3);
dd($rs);//1 Returns the number of rows affected
}
// Self-subtracting renewal
public function update(){
// All ages plus 1
$rs = DB::table('student')->decrement('age');
dd($rs);//5 Returns the number of rows affected
//ID For 1001 Age of plus 3
$rs = DB::table('student')
->where('id',1001)
->decrement('age',3);
dd($rs);//1 Returns the number of rows affected
}
//1001 Age plus 3 And the gender should be changed to 11
public function update(){
$rs = DB::table('student')
->where('id',1001)
->increment('age',3,['sex'=>11]);
dd($rs);//1 Returns the number of rows affected
}
Delete
// Delete ID For 1006 Data of
public function delete(){
$rs = DB::table('student')
->where('id',1006)
->delete();
dd($rs);//1 Returns the number of rows affected
}
// Delete ID Greater than 1003 Data of
public function delete(){
$rs = DB::table('student')
->where('id','>',1003)
->delete();
dd($rs);//2 Returns the number of rows affected
}
// Empty the data table and return nothing
DB::table('student')->truncate();
Query
get first pluck select
// Insert 1 Bar data and return self-increment ID
public function insert(){
$id = DB::table('student')->insertGetId([
'name'=>'Tom',
'age'=>11
]);
dd($id); //1004
}
0
// Insert 1 Bar data and return self-increment ID
public function insert(){
$id = DB::table('student')->insertGetId([
'name'=>'Tom',
'age'=>11
]);
dd($id); //1004
}
1
// Insert 1 Bar data and return self-increment ID
public function insert(){
$id = DB::table('student')->insertGetId([
'name'=>'Tom',
'age'=>11
]);
dd($id); //1004
}
2
// Query name , age , sex Field
$rs = DB::table('student')->select('name','age','sex')->get();
Aggregate function
// Insert 1 Bar data and return self-increment ID
public function insert(){
$id = DB::table('student')->insertGetId([
'name'=>'Tom',
'age'=>11
]);
dd($id); //1004
}
4
For more readers interested in Laravel related content, please check the topics on 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"
I hope this article is helpful to the PHP programming based on Laravel framework.