laravel分为三大数据库操作(DB facade[原始查找],查询构造器[Query Builder],Eloquent ORM):
1
| use Illuminate\Support\Facades\DB;
|
1.DB facade[原始查找]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| $results = DB::select('select * from users where id = :id', ['id' => 1]);
DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle']); 不返回值:
DB::statement('drop table users'); 返回自增id:
$id = DB::table('users')->insertGetId( ['email' => 'john@example.com', 'votes' => 0] );
$affected = DB::update('update users set votes = 100 where name = ?', ['John']); $num=DB::delete('delete from vipinfo where vip_ID= ?',[5]);
|
2.查询构造器[Query Builder]
laravel查询构造器提供了方便流畅的接口,用来建立及执行数据库查找语法。使用了pdo参数绑定,使应用程序免于sql注入,因此传入的参数不需要额外转义特殊字符。基本上可以满足所有的数据库操作,而且在所有支持的数据库系统上都可以执行。
(1)新增
1 2 3 4 5 6 7 8 9 10 11
| $bool=DB::table("vipinfo")->insert(['vip_ID'=>6,'vip_name'=>'zls','vip_type'=>"出行",'vip_fenshu'=>800]); echo $bool; $id=DB::table("vipinfo")->insertGetId(['vip_ID'=>5,'vip_name'=>'wyp','vip_type'=>"出行",'vip_fenshu'=>800]); echo $id; $bool=DB::table("vipinfo")->insert([ ['vip_ID'=>5,'vip_name'=>'wyp','vip_type'=>"出行",'vip_fenshu'=>800], ['vip_ID'=>6,'vip_name'=>'zls','vip_type'=>"出行",'vip_fenshu'=>800], ]); echo $bool;
|
(2)修改
1 2 3 4 5 6 7 8 9 10 11 12
| $bool=DB::table("vipinfo")->where('vip_ID',6)->update(['vip_fenshu'=>500]); echo $bool;
$bool=DB::table("vipinfo")->where('vip_ID',6)->increment("vip_fenshu"); $bool=DB::table("vipinfo")->where('vip_ID',6)->increment("vip_fenshu",3); echo $bool;
$bool=DB::table("vipinfo")->where('vip_ID',6)->decrement("vip_fenshu"); $bool=DB::table("vipinfo")->where('vip_ID',6)->decrement("vip_fenshu",3); echo $bool;
$bool=DB::table("vipinfo")->where('vip_ID',6)->increment("vip_fenshu",3,['vip_name'=>'dbdibi']);
|
(3)删除
1 2 3 4
| $num=DB::table("vipinfo")->where('vip_ID',6)->delete(); $num=DB::table("vipinfo")->where('vip_ID','>',4)->delete(); echo $num; $num=DB::table("vipinfo")->truncate();
|
(4)查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| $student=DB::table("vipinfo")->get(); var_dump($student);
$student=DB::table("vipinfo")->first(); $student=DB::table("vipinfo")->orderBy('vip_ID','desc')->first(); var_dump($student);
$student=DB::table("vipinfo")->where('vip_ID','>=',2)->get(); $student=DB::table("vipinfo")->whereRaw('vip_ID> ? and vip_fenshu >= ?',[2,300])->get(); dd($student);
$student=DB::table("vipinfo")->pluck('vip_name'); dd($student);
$student=DB::table("vipinfo")->lists('vip_name','vip_ID'); dd($student); $student=DB::table("vipinfo")->lists('vip_name');
$student=DB::table("vipinfo")->select('vip_name','vip_ID')->get(); dd($student);
$student=DB::table("vipinfo")->chunk(2,function($students){ var_dump($students); if(.......) return false; });
|
使用聚合函数
1 2 3 4 5 6 7 8 9 10 11 12
| $nums=DB::table("vipinfo")->count(); echo $nums;
$max=DB::table("vipinfo")->max("vip_fenshu"); echo $max;
$avg=DB::table("vipinfo")->avg("vip_fenshu"); echo $avg;
$sum=DB::table("vipinfo")->sum("vip_fenshu"); echo $sum;
|
3.Eloquent ORM
1.简介、模型的建立及查询数据
简介:laravel所自带的Eloquent ORM 是一个ActiveRecord实现,用于数据库操作。每个数据表都有一个与之对应的模型,用于数据表交互。
建立模型,在app目录下建立一个Student模型,即Student.php,不需要带任何后缀。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?php namespace App; use Illuminate\Database\Eloquent\Model; class Student extends Model{ protected $table= 'vipinfo'; protected $primaryKey= 'vip_ID'; protected $timestamps= false; protected $fillable= ['id','name'];
}
|
在Student控制器里增加一个test3方法,配置路由Route::get(‘test3’,[‘uses’=>‘StudentController@test3’]);
1 2 3 4 5 6 7 8 9 10 11
| public function test3(){ $studnets=Student::all(); dd($studnets); $student=Student::find(5); var_dump($student['attributes']); $student=Student::get(); var_dump($student); }
|
2 . 新增数据、自定义时间戳、批量赋值
(1)使用save方法新增
laravel会默认维护created_at,updated_at 两个字段,这两个字段都是存储时间戳,整型11位的,因此使用时需要在数据库添加这两个字段。如果不需要这个功能,只需要在模型里加一个属性:public $timestamps=false; 以及一个方法,可以将当前时间戳存到数据库
1 2 3
| protected function getDateFormat(){ return time(); }
|
这样就不需要那两个字段了。
控制器里写:
1 2 3 4 5 6 7
| $student=new Student();
$student->vip_name='xiaoming'; $student->vip_type='出行'; $student->vip_fenshu=900; $bool=$student->save(); echo $bool;
|
从数据库里取得某条记录的时间戳时,默认取得的是按日期格式化好的时间戳,如果想取得原本的时间戳,则在模型里增加asDateTime方法。
1 2 3
| protected function asDateTime($val){ return $val; }
|
(2)使用create方法新增时,需要在模型里增加:
1 2 3 4 5
| protected $fillable=['vip_name','vip_fenshu','vip_type']; 控制器里写:
Student::create(['vip_name'=>'mmm','vip_fenshu'=>999,'vip_type'=>'出行']); 这样即可新增成功!
|
(3)firstOrCreate()以属性查找记录,若没有则新增
1 2
| $student=Student::firstOrCreate(['vip_name'=>'mmm']); echo $student;
|
####(4)firstOrNew()以属性查找记录,若没有则会创建新的实例。若需要保存,则自己调用save方法()
1 2 3
| $student=Student::firstOrNew(['vip_name'=>'mmm']); $student->save(); echo $student;
|
3. 修改数据
使用save方法更新模型
使用update方法更新数据(和create相对应的,Eloquent模型类还支持使用update方法更新数据,同样要用到批量赋值)
1 2 3 4 5 6 7
| $student=Student::find(2); $student->vip_fenshu=10000; $student->save();
$num=Student::where('vip_ID','>',2)->update(['vip_fenshu'=>2000]); echo $num;
|
4. 删除数据
1 2 3 4 5 6 7 8
| $student=Student::find(11); $student->delete();
$num=Student::destroy(10); echo $num; $num=Student::destroy(10,5); echo $num;
|