laravel入门之增删改查
jianfly.com 2019-01-31 1595次浏览
<?php //配置 config/database.php .env use Illuminate\Support\Facades\DB; //原生语句 //查询 $student = DB::select('select * from student'); //新增 $bool = DB::insert('insert into student (name, age) values (?, ?)', ['sean', 18]); //修改 $num = DB::update('update student set age = ? where name = ?', [20, 'sean']); //删除 $num = DB::delete('delete from student where id > ?', [1001]); //查询构造器 //新增 $bool = DB::table('student')->insert($list); $id = DB::table('student')->insertGetId($list); $bool = DB::table('student')->insert([$list,$list,$list]); //更新 $num = DB::table('student')->where('id', 12)->update(['age'=>13]); $num = DB::table('student')->where('id', 12)->increment('age');//自增1 $num = DB::table('student')->where('id', 12)->increment('age',3);//自增3 $num = DB::table('student')->where('id', 12)->decrement('age');//自减1 $num = DB::table('student')->where('id', 12)->increment('age',3,['name'=>'iimooc']);//自减3同时修改 //删除 $num = DB::table('student')->where('id', 12)->delete(); $num = DB::table('student')->where('id', '>=', 12)->delete(); $num = DB::table('student')->truncate();//清空 //查询 $student = DB::table('student')->get();//所有 $student = DB::table('student')->orderBy('id', 'desc')->first();//第一条 $student = DB::table('student')->where('id', '>=', 1002)->get(); $student = DB::table('student')->whereRaw('id >= ? and age > ?', [1001, 18])->get(); $student = DB::table('student')->whereRaw('id >= ? and age > ?', [1001, 18])->pluck('name');//name字段 $student = DB::table('student')->whereRaw('id >= ? and age > ?', [1001, 18])->lists('name');//与pluck差不多 $student = DB::table('student')->whereRaw('id >= ? and age > ?', [1001, 18])->lists('name','id');//id为key $student = DB::table('student')->chunk(1000, function($students){ dd($student); });//分组查询 //函数 $student = DB::table('student')->count(); $student = DB::table('student')->max('age'); $student = DB::table('student')->min('age'); $student = DB::table('student')->avg('age'); $student = DB::table('student')->sum('age'); //ORM //建立student模型 namespace App; use Illuminate\Database\Eloquent\Model; class Student extends Model { //默认表为students //手动指定 protected $table = 'student'; //默认id为主键 protected $primaryKey = 'id'; //关闭自动维护时间戳 //public $timestamps = false; public $timestamps = true; protected function getDateFormat() { return time(); } //不自动格式化时间 protected function asDateTime($val){ return $val; } //指定允许批量赋值的字段 protected $fillable = ['name', 'age']; //指定不允许批量赋值的字段 protected $guarded = []; } //控制器中 $students = Student::all();//是一个Collection集合对象,返回表中所有 $student = Student::find(1001);//是一个Student对象 $student = Student::findOrFail(1001);//查不到报错 $students = Student::get();//返回Collection $students = Student::where('id','>','1001')->orderBy('age','desc')->first();//是一个Student对象 Student::chunk(2, function($students) { dd($students);//Collection集合 }) $num = Student::count(); //使用模型新增数据 $student = new Student();//Student对象 $student->name = 'sean'; $student->age = '18'; $bool = $student->save();//保存,会自动更新created_at和updated_at字段 $student = Student::find(1017); echo $student->created_at;//格式化的时间(可以关闭) //使用模型的Create方法新增数据 $student = Student::create( ['name'=>'imooc', 'age'=>18] );//需要设置才能批量添加,返回student对象 //firstOrCreate(); $student = Student::firstOrCreate( ['name'=>'imooc'] );//以属性查找,如果没有则新增 //firstOrNew() $student = Student::firstOrNew( ['name'=>'imooc'] );//以属性查找,如果没有则返回实例,如果需要保存需调用save $bool=$student->save() //使用ORM修改数据 //通过模型更新数据 $student = Student::find(1021); $student->name = 'kitty'; $bool = $student->save(); //批量 $num = Student::where('id', '>', 1019)->update( ['age'=>41] )//返回跟新条数 //使用ORM删除数据 //通过模型删除 $student = Student::find(1021); $bool = $student->delete(); //通过主键删除 $num = Student::destroy(1020); $num = Student::destroy(1018, 1019); $num = Student::destroy([1016, 1017]); //删除指定条件 $num = Student::where('id', '>', 1004)->delete();
- 上一篇:laravel入门之blade模板
- 下一篇:laravel入门之路由