Laravel 中提供了DB facade(原始查找)、查询构造器 和Eloquent ORM三种操作数据库方式
一、数据库操作之—DB facade:
新建数据表与连接数据库:
新建数据表:
CREATE TABLE IF NOT EXISTS student(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '姓名',
`age` TINYINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '年龄',
`sex` TINYINT UNSIGNED NOT NULL DEFAULT 10 COMMENT '性别',
`created_at` INT NOT NULL DEFAULT 0 COMMENT '新增时间',
`updated_at` INT NOT NULL DEFAULT 0 COMMENT '修改时间'
)ENGINE=INNODB DEFAULT CHARSET = UTF8 AUTO_INCREMENT=1001 COMMENT='学生表';连接数据库:
1.找到数据库的配置 config/database.php
'default' => env('DB_CONNECTION', 'mysql'), //查看默认数据库是否正确
//看mysql的具体配置是否正确
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
2.找env文件
修改配置
DB_HOST=localhost
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=root
使用DB facade实现CURD
StudentController中
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller{
public function test1(){
//插入
/*$bool = DB::insert('insert into student(name,age) value(?,?)',
['imooc',19]);
var_dump($bool);*/
//修改
/*$num = DB::update('update student set age = ? where name = ?',
[20,'sean']); //返回成功的行数
var_dump($num); */
//查询
/*$students = DB::select('select * from student');
dd($students);*/
//删除
$num = DB::delete('delete from student where id > ?',['1002']); //返回成功的行数
var_dump($num);
}
}
?>
二、数据库操作之—查询构造器
1.查询构造器简介及新增数据
● laravel查询构造器(query builder)提供方便、流畅的接口,用来建立及执行数据库查找语法。
● 使用PDO参数绑定,以保护应用程序免于SQL注入因此传入的参数不需额外转义特殊字符。
● 基本可以满足所有的数据库操作,而且在所有支持的数据库系统上都可以执行。
使用查询构造器新增:
//使用查询构造器插入数据
DB::table('student')->insert(array('name' => jason,'age' => 18)); //返回布尔类型
DB::table('student')->insertGetId(array('name' => jason,'age' => 18)); //返回插入的id
DB::table('student')->insert([
['name' => 'jason','age' => 18],
['name' => 'jason2','age' => 18]
]); //插入多条数据,返回布尔类型2.使用查询构造器修改数据
更新为指定的内容
//使用查询构造器更新数据
DB::table('student')->where('id', 12)->update(['age' => 18]); //返回影响的行数
自增和自减
DB::table('as_admin')->where('id', 12)->increment('age', 3); //字段自增3写,默认为1法 返回影响的行数
DB::table('as_admin')->where('id', 12)->decrement('age', 3); //字段自减3写,默认为1法 返回影响的行数
DB::table('as_admin')
->where('id', 12)
->increment('age', 3, ['name' => '张佳宁']); //自增或自减的同时更新name字段
3.使用查询构造器删除数据
● delete
● truncate
//使用查询构造器删除数据
public function query3(){
/*$num = DB::table('student')
->where('id',11)
->delete();*/
$num = DB::table('student')
->where('id', '>=', 9)
->delete();
var_dump($num);//返回删除的行数
DB::table('student')->truncate();//整表删除,这个操作很危险,谨慎使用,不返回任何数;
}
4.使用查询构造器查询数据
● get() ● lists()
● first() ● select()
● where() ● chunk()
● pluck()
//get() 获取表中所有数据
//$students = DB::table('student')->get();
//first() 获取第一条数据随机,常配合orderBy 一起使用
//$students = DB::table('student')->orderBy('id','asc')->first();
//where 多条件查询
/*$students = DB::table('student')
->whereRaw('id >= ? and age > ?',[18,20])
->get();*/
//pluck 取结果集中一列特定列,返回字符串类型
/*$students = DB::table('student')
->pluck('id','name','age');*/
//lists 按照Key=>value 对 的方式返回数组;最多两个参数,第一个参数作为value,第二个做为key。一个参数时与pluck用法一样
/*$students = DB::table('student')
->whereRaw('id >= ? and age > ?',[18,20])
->lists('id','name','age');*/
//select() 指定查询的字段
/*$students = DB::table('student')
->select('id','name','age')
->get();*/
//chunk() 方法 指定一次返回多少条,后跟闭包(匿名函数)
/*echo '<pre>'; //预格式化
DB::table('student')->chunk(2,function($students){
var_dump($students);
if(你的条件){
return false;
}
});*/
dd($students);5.查询构造器中的聚合函数
● count() ● avg()
● max() ● min()
● sum
//聚合函数
DB::table('student')->count(); //返回记录数
DB::table('student')->max('age'); //最大值,min同理
DB::table('student')->avg('age'); //返回平均值
DB::table('student')->sum('sum'); //求某一列和
345


— 数据库操作之 - DB façade和查询构造器&spm=1001.2101.3001.11974&articleId=70989659&d=1&t=3&u=e943ef6342e64ff380fde53ef96d35cc)

被折叠的 条评论
为什么被折叠?



