laravel架构设置(Laravel框架查询构造器 CURD操作示例)
时间:2021-10-24 10:30:16类别:编程学习
laravel架构设置
Laravel框架查询构造器 CURD操作示例
本文实例讲述了Laravel框架查询构造器 CURD操作。分享给大家供大家参考,具体如下:
新增
?
1
2
3
4
5
6
7
8
|
public function insert(){
$rs = DB::table( 'student' )->insert([
'name' => 'Kit' ,
'age' => 12
]);
dd( $rs );
}
|
?
1
2
3
4
5
6
7
8
|
public function insert(){
$id = DB::table( 'student' )->insertGetId([
'name' => 'Tom' ,
'age' =>11
]);
dd( $id );
}
|
?
1
2
3
4
5
6
7
8
|
public function insert(){
$rs = DB::table( 'student' )->insert([
[ 'name' => 'Ben' , 'age' =>22],
[ 'name' => 'Jean' , 'age' =>23]
]);
dd( $rs );
}
|
更新
?
1
2
3
4
5
6
7
|
public function update(){
$rs = DB::table( 'student' )
->where( 'id' ,1003)
->update([ 'age' =>10]);
dd( $rs );
}
|
?
1
2
3
4
5
6
7
8
9
10
11
|
public function update(){
$rs = DB::table( 'student' )->increment( 'age' );
dd( $rs );
$rs = DB::table( 'student' )
->where( 'id' ,1001)
->increment( 'age' ,3);
dd( $rs );
}
|
?
1
2
3
4
5
6
7
8
9
10
11
|
public function update(){
$rs = DB::table( 'student' )->decrement( 'age' );
dd( $rs );
$rs = DB::table( 'student' )
->where( 'id' ,1001)
->decrement( 'age' ,3);
dd( $rs );
}
|
?
1
2
3
4
5
6
7
|
public function update(){
$rs = DB::table( 'student' )
->where( 'id' ,1001)
->increment( 'age' ,3,[ 'sex' =>11]);
dd( $rs );
}
|
删除
?
1
2
3
4
5
6
7
|
public function delete (){
$rs = DB::table( 'student' )
->where( 'id' ,1006)
-> delete ();
dd( $rs );
}
|
?
1
2
3
4
5
6
7
|
public function delete (){
$rs = DB::table( 'student' )
->where( 'id' , '>' ,1003)
-> delete ();
dd( $rs );
}
|
?
1
2
|
DB::table( 'student' )->truncate();
|
查询
?
1
2
|
$rs = DB::table( 'student' )->get();
|
?
1
2
|
$rs = DB::table( 'student' )->orderBy( 'id' , 'desc' )->first();
|
?
1
2
3
4
|
$rs = DB::table( 'student' )->pluck( 'name' );
$rs = DB::table( 'student' )->pluck( 'name' , 'id' );
|
?
1
2
|
$rs = DB::table( 'student' )->select( 'name' , 'age' , 'sex' )->get();
|
聚合函数
?
1
2
3
4
5
|
$rs = DB::table( 'student' )-> count ();
$rs = DB::table( 'student' )->max( 'age' );
$rs = DB::table( 'student' )->min( 'age' );
$rs = DB::table( 'student' )->avg( 'age' );
$rs = DB::table( 'student' )->sum( 'age' );
|
希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。
原文链接:https://blog.csdn.net/qq_18335837/article/details/81287841