[笔记][Laravel]数据库操作

准备

新建App\Http\Controller\StudentController.php

<?php
namespace App\Http\Controller;
use Illuminate\Support\Facades\DB;//使用DB类

class StudentController extends Controller{
//创建名为StudentController的控制器并继承Controller基类
public function query(){
//php
}
}

切换至routes\web.php新建一个路由

[php]
Route::any(‘query’,’StudentController@query’);
[/php]

创建


连接数据库

配置.env文件

[php]
DB_CONNECTION=mysql[数据库的连接模式]
DB_HOST=数据库的IP地址
DB_PORT=数据库端口
DB_DATABASE=数据库库名
DB_USERNAME=数据库用户名
DB_PASSWORD=数据库密码
[/php]

配置config/database.php

[php]
‘mysql’ => [
‘driver’ => ‘mysql’, //数据库的类型
‘host’ => env(‘DB_HOST’, ‘localhost’), //数据库的位置
‘port’ => env(‘DB_PORT’, ‘3306’), //端口号
‘database’ => env(‘DB_DATABASE’, ”), //数据库名
‘username’ => env(‘DB_USERNAME’, ”), //用户名
‘password’ => env(‘DB_PASSWORD’, ”), //密码
‘charset’ => ‘utf8’, //字符集
‘collation’ => ‘utf8_unicode_ci’, //排序方式
‘prefix’ => ”, //前缀
‘strict’ => true, //Strict模式
‘engine’ => null, //引擎
],
[/php]

至此laravel即可连接上数据库了


使用DB facades完成数据库操作

SQL语句中的?是占位符,可以通过第二变量传入数组来传递数值

查询数据

[php]
DB::select(“select * from student”);//可以直接使用select语句
DB::select(“select * from student where age = ? and name = ?”,[18,’7gugu’])//等同于:select * from student where age = 18 and name = ‘7gugu’
[/php]

插入数据

[php]
DB::insert(“insert into student(name,age,sex) values(‘hzq’,’19’,’0′)”);//可以直接使用insert语句
DB::insert(“insert into student(name,age,sex) values(?,?,?)”,[‘hzq’,’19’,’0′]);//等同于:insert into student(name,age,sex) values(‘hzq’,’19’,’0′),这样子方便传值
[/php]

更新数据

[php]
DB::update(“update student set age = 1 where name =7gugu”);//可以直接使用update语句
DB::update(“update student set age = ? where name =?”,[1,’7gugu’])//等同于:update student set age = 1 where name =’7gugu’
[/php]

删除数据

[php]
DB::delete(‘delete from student where id=1002’);//直接使用delete语句
DB::delete(‘delete from student where id=?’,[‘1002’]);//等同于:delete from student where id=1002
[/php]


使用查询构造器

数据表指向器

[php]
DB::table(“数据表名”)->操作/筛选器;
DB::table(‘student’)->操作/筛选器;
[/php]

筛选器

[php]
DB::table(‘student’)->where()->操作;
where(‘id’,1001)//即可筛选id是1001的数据
where(‘id’,’=’,1001)//即可筛选id是1001的数据
where(‘id = ? and name = ?’,[1001,’7gugu’])//即可限定多个条件
[/php]

排序

[php]
/*
* @param string 字段名称
* @param string 排序方式(ASC:正序|DESC:倒序)
*/
DB::table(‘student’)->orderBy(‘字段名’,’asc|desc’)->操作;
[/php]

字段值自增

[php]
/*
* 数值自增
* @param string 字段名称
* @param int 单次自增数量
*/
DB::table(‘student’)->where(‘id’,’1001′)->increment(‘age’,1);
[/php]

字段值自减

[php]
/*
* 数值自减
* @param string 字段名称
* @param int 单次自减数量
*/
DB::table(‘student’)->where(‘id’,’1001′)->decrement(‘age’,1);
[/php]

插入数据

[php]
DB::table(‘student’)->insert([‘name’=>’7gugu’,’age’=>’18’]);//插入一条数据
DB::table(‘student’)->insert([
[‘name’=>’test’,’age’=>’20’],
[‘name’=>’guangzhou’,’age’=>’30’]
]);//使用一个数组来承载多条数据,即可插入多条数据
[/php]

更新数据

[php]
DB::table(‘student’)->where(‘id’,1004)->update([‘age’=>30]);//使用update函数即可更新指定字段的值
//laravel好像并不可以一次性更新多个字段
[/php]

删除数据

[php]
DB::table(‘student’)->where(‘id’,’1002′)->delete();
[/php]

get函数

[php]
/*
*用于获取限定条件后的数据,可以无限定条件直接获取
*/
DB::table(‘student’)->get();
[/php]

first函数

[php]
/*
* 用于获取符合限定条件的第一行的数据
* 无需传参
*/
DB::table(‘student’)->first();
[/php]

pluck函数

[php]
/*
* 获取指定名称的字段数据
*/
DB::table(‘student’)->pluck(‘name’,’age’);
[/php]

效果如图所示:

聚合函数

[php]
//count()统计表的数据总数
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’)->avg(‘age’);
[/php]

truncate函数

清空表中数据[危险]

[php]
//truncate()清空表中数据[危险]
DB::table(‘student’)->truncate();
[/php]

chunk函数

用于分块查找数据(用于大量数据查找时使用)

[php]
DB::table(‘users’)->chunk(2, function($users)
{
dd($users);//每次运行仅仅查询两条数据,直至查询完毕
});
[/php]

结果如图所示:

作者: 7gugu

I'm a phper!

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注