Laravel数据库操作之-Eloquent ORM
来源:互联网 发布:电子商务网络环境 编辑:程序博客网 时间:2024/05/21 23:23
数据库操作之-Eloquent ORM
- Eloquent ORM简介、模型的建立及查询数据
- Eloquent ORM中新增数据、自定义时间戳及批量赋值的使用
- 使用Eloquent ORM修改数据
- 使用Eloquent ORM删除数据
1. Eloquent ORM简介、模型的建立及查询数据
- Eloquent ORM简介
- Laravel所自带的Eloquent ORM是一个优美、简介的ActiveRecord实现,用来实现数据库操作
- 每个数据表都有一个与之相对应的“模型(model)”用于和数据表交互
- 模型的建立
- 新建模型Student.php,代码如下:
模型文件所在位置laravel\app\Student.php
<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Student extends Model{ // 默认表名students // 手动指定表名 protected $table = 'student'; // 默认是id字段作为主键,指定id为主键 protected $primaryKey = 'id';}
- Eloquent ORM中的查询
- all()、find()、findOrFail()
- 查询构造器在ORM中的使用
// 引入model模型use App\Student;// all() 返回是一个集合// $students = Student::all();// find()// $student = Student::find(1001);// findOrFail() 根据主键查找如果没有找到就报错// $student = Student::findOrFail(1006);// $students = Student::get();// $student = Student::where('id','>','1001')// ->orderBy('age','desc')// ->first();// echo '<pre>';// Student::chunk(2,function($students){// var_dump($students);// });// 聚合函数// $num = Student::count();$max = Student::where('id','>',1001)->max('age');var_dump($max);
2. Eloquent ORM中新增数据、自定义时间戳及批量赋值的使用
- 通过模型新增数据(涉及到自定义时间戳)
- 使用模型的Create方法新增数据(涉及到批量赋值)
模型文件所在位置laravel\app\Student.php
<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Student extends Model{ // 默认表名students // 手动指定表名 protected $table = 'student'; // 默认是id字段作为主键,指定id为主键 protected $primaryKey = 'id'; // 指定允许批量赋值的字段 protected $fillable = ['name','age']; // 指定不允许批量赋值的字段 protected $guarded = []; // 自动维护时间戳 public $timestamps = true; // 插入时间戳 protected function getDateFormat() { return time(); } protected function asDateTime($val) { return $val; }}
- 使用模型的Create方法新增数据(涉及到批量赋值)
// 使用模型新增数据// $student = new Student();// $student->name = 'sean2';// $student->age = 20;// $bool = $student->save();// dd($bool);// 查询时间戳// $student = Student::find(1010);// echo date('Y-m-d H:i:s',$student->created_at);// 使用模型的Create方法新增数据// $student = Student::create(// ['name'=>'imooc','age'=>18]// );// firstOrCreate() 查询数据库中字段的属性,如果没有,则新增一条// $student = Student::firstOrCreate(// ['name'=>'imoocs']// );// firstOrNew() 以属性查找数据库,没有则生成新的实例,需要保存调用save()方法$student = Student::firstOrNew( ['name'=>'imoocss'] );$bool = $student->save();dd($bool);
3. 使用Eloquent ORM修改数据
- 通过模型更新
- 结合查询语句批量更新
// 通过模型更新数据// $student = Student::find(1013);// $student->name = 'kity';// $bool = $student->save();// var_dump($bool);$num = Student::where('id','>',1011)->update( ['age'=>41] );var_dump($num);
4. 使用Eloquent ORM删除数据
- 通过模型删除
- 通过主键值删除
- 根据指定条件删除
// 通过模型删除 // $student = Student::find(1013);// $bool = $student->delete();// var_dump($bool);// 通过主键删除// $num = Student::destroy(1012);// $num = Student::destroy(1010,1011);// $num = Student::destroy([1008,1009]);$num = Student::where('id','>',1004)->delete();var_dump($num);
0 0
- Laravel数据库操作之-Eloquent ORM
- laravel的Eloquent ORM操作
- 3.关于laravel框架中数据库操作-Eloquent ORM
- laravel学习笔记之Eloquent ORM
- Laravel Eloquent ORM--整理
- laravel Eloquent ORM
- Laravel Eloquent ORM--整理
- Laravel Eloquent ORM--整理
- Laravel学习笔记(六)---操作数据库--Eloquent ORM(对象关系映射)
- Laravel学习笔记(七)---操作数据库--Eloquent ORM--关联关系及其在模型中的定义
- Laravel基础-Eloquent ORM 模型
- laravel笔记-Eloquent ORM(基础)
- laravel笔记-Eloquent ORM(杂项)
- Laravel Eloquent ORM 一种高级用法
- 在 CodeIgniter 中使用 Laravel Eloquent ORM
- Laravel Eloquent ORM的hasOne和belongsTo
- Laravel Eloquent ORM 多条件查询
- Laravel 5.4 官方文档摘记:Eloquent ORM
- 干货 | 你真的了解 Convolutional Neural Networks 么
- 干货 | Generative Adversarial Networks(GAN)的现有工作
- Laravel数据库操作之-DB facade
- Laravel数据库操作之-增删改查CURD操作
- 编码(一):编码简介
- Laravel数据库操作之-Eloquent ORM
- Laravel-Blade模板引擎
- Laravel-Blade模板引擎-2.基础语法及include的使用
- Laravel-Blade模板引擎-3.流程控制
- Laravel-Blade模板引擎-4. 模板中的URL
- 数组、List和ArrayList的区别
- 1st round, 311 Sparse Matrix Multiplication
- 使用iostat和LLD实现zabbix监控IO性能
- 自定义vimrc