laravel5.6 ORM 关联模型,一对一和一对多
2024-10-14 06:53:40
Laravel5.6 关联模型的操作,主要是一对一,一对多,多对多等操作.下面示例主要解析前面两个操作用法比较常用.(操作和用法TP5类似)
将关联查询使用语法hasOne、hasMany、belongsTo进行一个举例说明?
hasOne:有一个,加上主谓语应该是, A 有一个 B
hasMany:有很多, A 有很多 B
belongsTo:属于, A 属于 B demo示例:
假设Users模型和News模型存在关联关系.两表sql和假设数据如下:
users.sql sql文件
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` char(30) NOT NULL DEFAULT '' COMMENT '名称',
`created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
`updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表'; -- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES ('', 'admin_01', '2018-11-19 10:06:17', '2018-11-19 10:06:22');
INSERT INTO `users` VALUES ('', 'admin_02', '2018-11-19 10:09:27', '2018-11-19 10:09:34');
INSERT INTO `users` VALUES ('', 'admin_03', '2018-11-19 11:36:56', '2018-11-19 11:37:00');
news.sql sql文件
CREATE TABLE `news` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`users_id` int(10) NOT NULL DEFAULT '' COMMENT '用户表id',
`author` char(30) NOT NULL DEFAULT '' COMMENT '作者',
`content` text NOT NULL COMMENT '内容',
`created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
`updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='新闻表'; -- ----------------------------
-- Records of news
-- ----------------------------
INSERT INTO `news` VALUES ('', '', ' Ning', 'Hello World 01', '2018-11-19 10:08:04', '2018-11-19 10:07:59');
INSERT INTO `news` VALUES ('', '', 'Wang', 'Hello World 02', '2018-11-19 10:11:01', '2018-11-19 10:11:08');
INSERT INTO `news` VALUES ('', '', 'Li', 'Hello World 03', '2018-11-19 11:37:57', '2018-11-19 11:37:59');
INSERT INTO `news` VALUES ('', '', 'Hong', 'Hello World 04', '2018-11-19 15:02:26', '2018-11-19 15:02:29');
一对一:
例如:一个 Users 模型有一个与之关联的 News 模型.
Users.php Users模型
<?php namespace App\Model\Eloquent\Admin; use Illuminate\Database\Eloquent\Model; class Users extends Model
{
protected $table = 'users'; /**
* 关联news表
*/
public function newsMethod()
{
//hasOne($related, $foreignKey = null, $localKey = null)
//第一个参数为对应的model, 第二个参数默认为model对应的表的外键, 第三个参数默认为当前模型对应的表的主键
return $this->hasOne('App\Model\Eloquent\Admin\News','users_id', 'id');
}
}
News.php News模型
<?php namespace App\Model\Eloquent\Admin; use Illuminate\Database\Eloquent\Model; class News extends Model
{
protected $table = 'news'; /**
* 相对关联users表
*/
public function usersMethod()
{
//belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null)
//第一个参数为model, 先讲第四个参数,默认为调用belongsTo()的方法名字, 第二个参数默认为第四个参数加上 '_id', 第三个参数默认为model的对应表的主键
return $this->belongsTo('App\Model\Eloquent\Admin\Users','users_id','id');
}
}
关联查询 -- 查询news表关联id=1,一条数据
$data = Users::find(1)->newsMethod->toArray();
结果打印:
相对关联查询 -- 查询users表关联id=2,一条数据
$data = News::find(2)->usersMethod->toArray();
结果打印:
一对多
一对多关联:是定义单个模型拥有多个其它模型的关联关系.
Users.php Users模型
<?php namespace App\Model\Eloquent\Admin; use Illuminate\Database\Eloquent\Model; class Users extends Model
{
protected $table = 'users'; /**
* 关联news表 假设一对多
*/
public function newsMethodMany()
{
return $this->hasMany('App\Model\Eloquent\Home\News','users_id', 'id');
}
}
关联查询 -- 查询users和news两表关联id=3,两条数据
$data = Users::find(3)->newsMethodMany->toArray();
结果打印:
查询存在的关联关系操作语法.
with:类似于 SQL 中的 left join 注:渴求加载查指定字段, id字段是必须列出
$data = News::with('usersMethod:id,name')->get()->toArray();
结果打印:
has:类似于 SQL 中的 inner join
$data = News::has('usersMethod')->get()->toArray();
whereHas:inner join 之后,可以补充查询条件
$data = News::whereHas('usersMethod', function ($query) {
$query->where('author', '=', 'Hong');
})->get()->toArray();
结果打印:
最新文章
- Worktile 技术架构概要
- Clang Format
- ubuntu set ntpdate
- Angularjs调用公共方法与共享数据
- Delphi Application.MessageBox详解
- POJ 3258 River Hopscotch 二分枚举
- Tornado自定义分布式session框架
- IPv6被拒如何破?-b
- hadoop Yarn 编程API
- mysql中的timestamp类型时间比较:unix_timestamp函数
- Python框架
- CDockablePane 记忆界面布局的问题
- C#实现倒油算法
- 2.postman安装及使用
- Django---ORM框架
- Spring中@Component的作用
- iOS: Designated Initializer(指定初始化函数)
- Mac上Hive环境搭建
- LeetCode 4.反转整数
- NO.2day 操作系统基础