数据的插入:
//单条数据添加
// $res = DB::table('student')->insert(
// ['name'=>'yupangzi','age'=>25,'sex'=>2]
// );
//单挑数据添加并且返回添加id
// $res = DB::table('student')->insertGetId(
// ['name'=>'yupangzix','age'=>25,'sex'=>2]
// );
//多条数据添加
$res = DB::table('student')->insert([
['name'=>'yupangzix','age'=>25,'sex'=>2],
['name'=>'yupangzixx','age'=>25,'sex'=>2],
['name'=>'yupangzixxx','age'=>25,'sex'=>2]
] );
数据的修改:
// $res = DB::table('student')
// ->where('id',4)
// ->update(['age'=>999]);
//自增
// DB::table("student")->increment('age',3);
//自减
//DB::table("student")->decrement('age',3);
//自增并修改其他
$num = DB::table('student')
->where("id",5)
->increment('age',3,['name'=>'yangmouhui']);
数据的删除:
//delete truncate
// $res = DB::table('student')
// ->where('id','<=',5)
// ->delete();
// print_r($res);
$res = DB::table('student')->truncate();
数据的查询:
/*
* get() first() where() pluck() select()
* chunk()
*/
//get() 获取表数据
//$res = DB::table('student')->get();
//first 获取结果集第一条
// $res = DB::table('student')
// ->orderBy('id','desc')
// ->first();
// $res = DB::table('student')
// ->where('id','<',5)
// ->orderBy('id','desc')
// ->get();
//where多条件
// $res = DB::table("student")
// ->whereRaw('id<=? and age <? ',[10,26])
// ->get();
//pluck返回结果集的指定字段 1个
// $res = DB::table("student")
// ->pluck('name');
//age键名 name键值 自动去重复
// $res = DB::table("student")
// ->pluck('name','age');
//select
// $res = DB::table("student")
// ->select('id','name','age')
// ->get();
//一次性查询n条
// echo '<pre>';
// $res = DB::table("student")->chunk(2,function ($s){
// var_dump($s);
// if(1){
// return false;
// }
// });
聚合函数:
//聚合函数 count max min avg sum
$res = DB::table('student')->count();
var_dump($res);
$res = DB::table('student')->max('age');
$res = DB::table('student')->min('age');
$res = DB::table('student')->avg('age');
$res = DB::table('student')->sum('age');
var_dump($res);
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;
protected function getDateFormat()
{
// return parent::getDateFormat(); // TODO: Change the autogenerated stub
return time();
}
protected function asDateTime($value)
{
return $value;
}
//指定允许批量数值的字段
protected $fillable = ['name','age'];
}
// //all()
// $student = Student::all();
//
// //find
// $student = Student::find(1);
//
// //findOrFail
// $student = Student::findOrFail(1111);
// $res = Student::get();
// $res = Student::where('id','>','10')
// ->orderBy('age','desc')
// ->first();
// echo '<pre>';
// $res = Student::chunk(2,function($student){
// var_dump($student);
// });
$res =Student::count();
$res =Student::max('age');
$res =Student::min('age');
$res =Student::avg('age');
$res =Student::sum('age');
ORM的新增 自定义时间戳 以及批量赋值
通过模型新增数值
//模型新增数据
$s = new Student();
$s->name = 'yule';
$s->age = 65;
$s->save();
//时间戳
public $timestamps = true;
protected function getDateFormat()
{
// return parent::getDateFormat(); // TODO: Change the autogenerated stub
return time();
}
protected function asDateTime($value)
{
return $value;
}
通过模型的Create方法新增
Student::create(
['name'=>'immoc','age'=>18]
);
//firstOrCreate 根据属性查找 找不到创建并保存
Student::firstOrCreate([
'age'=>19,
'name'=>'hui'
]);
//firstOrNew 根据属性查找 找不到则创建 要保存需要调用save
$s = Student::firstOrCreate([
'age'=>19,
'name'=>'hui'
]);
$s->save();
//模型修改
$stu = Student::find(2);
echo $stu->name;
$stu->name = 'hello kitty';
$stu->save();
结合查询语句批量更新
$num = Student::where('id','>','20')->update(
['age'=>50]
);
echo $num;
模型删除
$student = Student::find(28);
$student->delete();
主键删除
Student::destroy(29);
echo $num = Student::destroy([24,25,29]);
指定定条件删除
echo Student::where('id','>',10)->delete();
//Eloquent ORM
public function orm_select(){
//all()
//$arr = Student::all();
//find()//$arr = Student::find(1111);
//findOrFail() 找不到抛出异常
//$arr = Student::findOrFail(111);
// $arr = Student::where('id','>','10')->first();
// $arr = Student::chunk(3,function($s){
// echo '<hr><pre>';
// print_r($s);
// echo '</pre>';
// });
// $arr =Student::count();
// $arr =Student::max('age');
// $arr =Student::min('name');
// $arr =Student::avg('age');
//$arr =Student::sum('age');
// $arr = Student::increment('age',2);
$s = Student::find(26);
echo $s->created_at;
// dd($arr);
}
public function orm_insert(){
// $student = new Student();
// $student ->name ='yuluozi1';
// $student ->age ='15';
// //$student ->crea
// $bool = $student->save();
//批量create 需要设置模型的fillable
// $bool= Student::create(
// ['name'=>'xiaohui','age'=>12]
// );
//firstOrCreate查不到 就创建
$bool = Student::firstOrCreate(
['name'=>'zhaoxl2']
);
//firstOrNew查不到 就初始化 需要手动save()才可以入库
$bool = Student::firstOrCreate(
['name'=>'zhaoxl2']
);
dd($bool);
}
public function orm_update(){
//通过模型更新数据 查询更新
// $s = Student::find(28);
// $s->name = 'Kate1';
// $bool = $s->save();
//条件批量更新
// $num = Student::where('id','>','15')->update(['age'=>16]);
// dd($num);
//通过对象更新数据 查询更新
$s = new Student();
$s1 =$s->find(28);
$s1->name = 'Jack';
$bool = $s1->save();
}
public function orm_delete(){
//模型删除
// $s = Student::find(16);
// $bool = $s->delete();
// dd($bool);
//主键删除 单个删除多个删除
// $bool = Student::destroy(17);
// $bool = Student::destroy(21,22,23);
// $bool = Student::destroy([18,19,20]);
//where条件删除
$bool = Student::where('id','>',30)->delete();
dd($bool);
}