一, DB facade(原始查找)
- // 查询
- $objectArray=DB::select('select * from student');
- foreach ($objectArray as $obj){
- echo $obj->id;
- }
- // 插入
- $bool=DB::insert('insert into student(name,age) values(?,?)',['tom',18]);
- // 修改
- $num=DB::update('update student set age=? where name=?',[20,'tom']);
- // 删除
- $num=DB::delete('delete from student where id=?',[1001]);
二, 查询构造器
Laravel 查询构造器提供了方便流畅的接口, 用来建立及执行数据库查找语法. 使用了 pdo 参数绑定, 使应用程序免于 sql 注入, 因此传入的参数不需要额外转义特殊字符. 基本上可以满足所有的数据库操作, 而且在所有支持的数据库系统上都可以执行.
查询
- // 查询所有数据
- $objectArray=DB::table('student')->get()->toArray();
- foreach ($objectArray as $object){
- echo $object->id;
- }
- // 根据条件查询数据
- $objectArray=DB::table('student')->where('id','>',1001)->get()->toArray();
- foreach ($objectArray as $object){
- echo $object->id;
- }
- // 根据多个条件查询数据
- $objectArray=DB::table('student')->whereRaw('id> ? and age = ?',[1001,15])->get()->toArray();
- foreach ($objectArray as $object){
- echo $object->id;
- }
- // 取出第一条数据 (升序 / 降序)
- $object=DB::table('student')->orderBy('id','desc')->first();
- echo $object->id;
- // 查询指定字段名 (可以指定字段名作为数组下标, 不指定默认数字下标, pluck 代替 lists 方法)
- $names=DB::table('student')->pluck('name','id')->toArray();
- // 查询指定的一个或多个字段
- $objectArray=DB::table('student')->select('id')->get()->toArray();
- // 根据指定记录条数查询数据并可以执行相应的方法
- DB::table('student')->orderBy('id','desc')->chunk(2,function ($objects){
- foreach ($objects as $object){
- if ($object->id==1004){
- echo 'find';
- }
- }
- });
插入
- // 单条插入
- $bool=DB::table('student')->insert(
- ['name'=>'tom','age'=>18]
- );
- // 插入并获取 id
- $id=DB::table('student')->insertGetId(
- ['name'=>'john','age'=>10]
- );
- // 多条插入
- $bool=DB::table('student')->insert([
- ['name'=>'ke1','age'=>12],
- ['name'=>'he1','age'=>19]
- ]);
修改
- // 单条修改
- $num=DB::table('student')
- ->where('id',1002)
- ->update(
- ['age'=>50]
- );
- // 运行此条语句自增 (默认 1)
- $num=DB::table('student')->increment('age');
- // 运行此条语句自增 (自增 3)
- $num=DB::table('student')->increment('age',3);
- // 运行此条语句自减 (默认 1)
- $num=DB::table('student')->decrement('age');
- // 运行此条语句自减 (自减 3)
- $num=DB::table('student')->decrement('age',3);
- // 根据条件自减
- $num=DB::table('student')
- ->where('id',1002)
- ->decrement('age',3);
- // 根据条件自减并修改字段
- $num=DB::table('student')
- ->where('id',1002)
- ->decrement('age',3,['name'=>'ioc']);
删除
- // 单条删除
- $num=DB::table('student')->where('id',1003)->delete();
- // 根据条件删除
- $num=DB::table('student')->where('id','>=',1005)->delete();
- // 删除整个表
- $num=DB::table('student')->truncate();
聚合函数
- // 统计记录条数
- $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');
三, Eloquent ORM
Laravel 所自带的 Eloquent ORM 是一个 ActiveRecord 实现, 用于数据库操作. 每个数据表都有一个与之对应的模型, 用于数据表交互
先新建一个 model 类文件, 内容如下:
- namespace App;
- use Illuminate\Database\Eloquent\Model;
- class Student extends Model
- {
- // 指定数据库表名
- protected $table='student';
- // 指定主键
- protected $primaryKey='id';
- // 自动维护时间戳
- public $timestamps = true;
- // 指定允许批量赋值的字段 (使用 create 方法批量增加时, 需要指定允许的字段)
- protected $fillable=['name','age'];
- // 指定不允许批量赋值的字段
- protected $guarded=[];
- // 自动格式化时间
- protected function getDateFormat()
- {
- return time();
- }
- // 直接返回时间戳 (getDateFormat 和 asDateTime 同时存在, asDateTime 生效)
- protected function asDateTime($value)
- {
- return $value;
- }
- }
接着, 在控制器里面调用新建的 model 类
查询
- // 查询所有数据
- $array=Student::all()->toArray();
- // 根据主键查询
- $array=Student::find(1001)->toArray();
- // 查不到记录报错
- $array=Student::findOrFail(101)->toArray();
- // [查询构造器] 查询所有数据, 在 ORM 中省略指定表名, 其余用法一致
- $array=Student::get()->toArray();
插入
- // 模型新增数据
- $student=new Student();
- $student->name='yy';
- $student->age=13;
- $bool=$student->save();
- // 模型 create 方法批量新增数据
- $object=Student::create(
- ['name'=>'ui','age'=>13]
- );
- // 以属性查找记录, 若无则新增
- $object=Student::firstOrCreate(
- ['name'=>'tom']
- );
- // 以属性查找记录, 若无则创建新实例, 若需要保存到数据库则需手动 save()
- $object=Student::firstOrNew(
- ['name'=>'tom2']
- );
- $bool=$object->save();
- // [查询构造器] 插入数据
- $bool=Student::insert(
- ['name'=>'mary','age'=>18]
- );
修改
- // 模型修改数据
- $object=Student::find(1025);
- $object->name='kero';
- $bool=$object->save();
- // [查询构造器] 根据条件修改
- $num=Student::where('id','=',1025)->update(
- ['age'=>10]
- );
删除
- // 模型删除数据
- $object=Student::find(1025);
- $bool=$object->delete();
- // 通过主键删除 (也可数组形式)
- $num=Student::destroy(1019,1020);
- // [查询构造器] 根据条件删除
- $num=Student::where('id','>',1016)->delete();
来源: http://www.bubuko.com/infodetail-2646454.html