Laravel之Eloquent ORM
一、ORM编程思想
1.1 Active Record 设计模式
Active Record 是一种数据访问设计模式,它可以帮助你实现数据对象Object到关系数据库的映射。应用Active Record时,每一个类的实例对象唯一对应一个数据库表的一行(一对一关系)。你只需继承一个abstract Active Record 类就可以使用该设计模式访问数据库,其最大的好处是使用非常简单
1.2 调试工具 Laravel Debugbar
Installation:
composer require barryvdh/laravel-debugbar --dev
二、一对一关系映射
2.1 创建表
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->increments('id');
$table->string('phone');
$table->unsignedInteger('user_id');
//显示的声明外键:通知数据库根据外键关联表和建立索引,提高运行速度
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->timestamps();
});
}
2.2 创建模型关系
2.2.1 正向关系绑定
public function profile()
{
return $this->hasOne(Profile::class);
}
2.2.2 反向关系绑定
public function user()
{
return $this->belongsTo(User::class);
}
2.3 外键
自定义外键:
return $this->hasOne(Profile::class,'显示指定自定义外键');
2.4 一对一测试
依赖注入Request $request,获取当前登录用户$request->user()
Route::get('/test',function (Request $request){
//反向
// $profile = \App\Profile::find(1);
// dd($profile->user);
$user = $request->user();
// if (is_null($user->profile)){
// $user->profile()->create([
// 'phone' => '15801340269'
// ]);
// }
//用firstOrCreate改进if
$user->profile()->firstOrCreate(['user_id' => $user->id],[
'phone' => '18363046291'
]);
//访问属性一样访问方法
dd($user->profile);
});
三、一对多关系映射
1:N hasMany(XXX:class)
反之:belongsTo(XXX:class)
3.1 面向对象方式绑定一对多的关系
四、多对多关系映射
中间表命名:按照A-Z首字母排序
public function users()
{
return $this->belongsToMany(User::class);
}
public function habits()
{
return $this->belongsToMany(Habit::class);
}
4.1 面向对象方式绑定多对多的关系
detach解绑,sync方法用的比较多,只保留1,2
4.2 访问多对多中间数据表
五、HasManyThrough对象桥接式穿越关联(远层一对多)
数据表:
countries
id - integer
name - string
users
id - integer
country_id - integer
name - string
posts
id - integer
user_id - integer
title - string
class Country extends Model
{
protected $fillable = ['name'];
/**
* 获得某个国家下所有的用户文章。
*/
public function papers()
{
return $this->hasManyThrough(Paper::class,User::class);
}
}
$factory->define(App\Paper::class, function (Faker $faker) {
return [
'title' => $faker->sentence,
'user_id' => \App\User::all()->random()->id,
];
});
$factory->define(App\User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'country_id' => \App\Country::all()->random()->id,
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
'remember_token' => str_random(10),
];
});
获取每个国家论文总数:
五、多样化的一对多关系映射(多态关联)
面向对象多态:运行时加载机制
伪造数据:
六、多对多多态关联
除了传统的多态关联,您也可以定义「多对多」的多态关联。例如,Post 模型和 Video 模型可以共享一个多态关联至 Tag 模型。 使用多对多多态关联可以让您在文章和视频中共享唯一的标签列表。
Laravel之Eloquent ORM的更多相关文章
- laravel通过Eloquent ORM实现CURD
//Eloquent ORM public function orm1() { //all(); 返回所有数据: /*$students=Student::all(); dd($students);* ...
- Laravel使用Eloquent ORM操作数据库
1.定义模型 <?php namespace App; use Illuminate\Database\Eloquent\Model; class Flight extends Model{ p ...
- Laravel之Eloquent ORM访问器调整器及属性转换
一.查询构建器的get方法 查询构建器的get方法返回了一个集合 $users = App\User::where('active', 1)->get(); foreach ($users as ...
- Laravel之Eloquent ORM关联
一.一对一 1.主对从(hasOne) 从User模型中取出用户的手机 User模型中: /** * 获取关联到用户的手机 */ public function phone() { return $t ...
- laravel 5.1 使用Eloquent ORM 操作实例
Laravel 的 Eloquent ORM 提供了更优雅的ActiveRecord 实现来和数据库的互动. 每个数据库表对应一个模型文件. 数据库配置 .env文件(也可以直接修改config/da ...
- Laravel Eloquent ORM
Eloquent ORM 简介 基本用法 集体赋值 插入.更新.删除 软删除 时间戳 查询范围 关系 查询关系 预先加载 插入相关模型 触发父模型时间戳 与数据透视表工作 集合 访问器和调整器 日期调 ...
- [转]Laravel 4之Eloquent ORM
Laravel 4之Eloquent ORM http://dingjiannan.com/2013/laravel-eloquent/ 定义Eloquent模型 模型通常放在app/models目录 ...
- Laravel 数据库操作 Eloquent ORM
laravel 操作数据库一般都使用它的Eloquent ORM才操作 建立模型 <?php namespace App; use Illuminate\Database\Eloquent\Mo ...
- [Laravel] 03 - DB facade, Query builder & Eloquent ORM
连接数据库 一.Outline 三种操作数据库的方式. 二.Facade(外观)模式 Ref: 解读Laravel,看PHP如何实现Facade? Facade本质上是一个“把工作推给别人做的”的类. ...
随机推荐
- 《DSP using MATLAB》Problem 4.3
代码: %% ------------------------------------------------- %% 1 x(n)=2δ(n-2)+3u(n-3) %% -------------- ...
- FabricExpress.net supply high quality quilting fabric
FabricExpress is a company specializing in high quality custom t-shirts,custom fabric,senior handmad ...
- Oracle 多行合并一行 方法
假如有如下表,其中各个i值对应的行数是不定的 SQL> select * from t; I A D ---------- ---------- --------------- ...
- docker 知识点
docker 教程:http://www.runoob.com/docker/docker-tutorial.html docker 仓库地址:https://store.docker.com/ do ...
- LINQ TO SQL 中的join(转帖)
http://www.cnblogs.com/ASPNET2008/archive/2008/12/21/1358152.html join对于喜欢写SQL的朋友来说还是比较实用,也比较容易接受的东西 ...
- webpack 基本使用
1. 创建webpack-test文件夹 2. npm初始化 3. 安装webpack 4. 使用webpack打包 hello.js 是需要打包的文件 hello.bundle.js 是打包完以后 ...
- valgrind的使用--检测内存
valgrind主要检测内存的使用情况,检测有否内存泄露等. 比如:test_va2.cpp #include<iostream> using namespace std; int mai ...
- MySqli 执行多条SQL语句
使用multi_query(); 去执行SQL语句,执行多条语句多个SQL语句用“;”分开 一:没有结果集的语句: $sql="insert into products (cid,name ...
- 开始转型学习java
什么编程语言这些都是一样的,编程思想都是一样的.只不过是表现形式. 标识符 每一个字符在ascll码表例都有对应的数字 所以字符和数字是可以相加的 'a'+1 也可以显示数字对应的字符 (ch ...
- flutter初探
这两天看了下flutter,感觉这两年可能会爆发,所以尝试在mac和win10上面跑了下hello world... 移动技术简介 原生开发 跨平台技术简介 H5+原生(Cordova.Ionic.微 ...