PHP头条
热点:

laravel5.6ORM 关联模型,一对一和一对多


Laravel5.6 关联模型的操作,主要是一对一,一对多,多对多等操作.下面示例主要解析前面两个操作用法比较常用.(操作和用法TP5类似)

将关联查询使用语法hasOne、hasMany、belongsTo进行一个举例说明?
hasOne:有一个,加上主谓语应该是, A 有一个 B
hasMany:有很多, A 有很多 B
belongsTo:属于, A 属于 B

demo示例:
假设Users模型和News模型存在关联关系.两表sql和假设数据如下:
users.sql  sql文件
 1 DROP TABLE IF EXISTS `users`;
 2 CREATE TABLE `users` (
 3   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 4   `name` char(30) NOT NULL DEFAULT '' COMMENT '名称',
 5   `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
 6   `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
 7   PRIMARY KEY (`id`)
 8 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
 9 
10 -- ----------------------------
11 -- Records of users
12 -- ----------------------------
13 INSERT INTO `users` VALUES ('1', 'admin_01', '2018-11-19 10:06:17', '2018-11-19 10:06:22');
14 INSERT INTO `users` VALUES ('2', 'admin_02', '2018-11-19 10:09:27', '2018-11-19 10:09:34');
15 INSERT INTO `users` VALUES ('3', 'admin_03', '2018-11-19 11:36:56', '2018-11-19 11:37:00');
news.sql  sql文件
 1 CREATE TABLE `news` (
 2   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 3   `users_id` int(10) NOT NULL DEFAULT '1' COMMENT '用户表id',
 4   `author` char(30) NOT NULL DEFAULT '' COMMENT '作者',
 5   `content` text NOT NULL COMMENT '内容',
 6   `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
 7   `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
 8   PRIMARY KEY (`id`)
 9 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='新闻表';
10 
11 -- ----------------------------
12 -- Records of news
13 -- ----------------------------
14 INSERT INTO `news` VALUES ('1', '1', ' Ning', 'Hello World 01', '2018-11-19 10:08:04', '2018-11-19 10:07:59');
15 INSERT INTO `news` VALUES ('2', '2', 'Wang', 'Hello World 02', '2018-11-19 10:11:01', '2018-11-19 10:11:08');
16 INSERT INTO `news` VALUES ('3', '3', 'Li', 'Hello World 03', '2018-11-19 11:37:57', '2018-11-19 11:37:59');
17 INSERT INTO `news` VALUES ('4', '3', 'Hong', 'Hello World 04', '2018-11-19 15:02:26', '2018-11-19 15:02:29');
一对一:
例如:一个 Users 模型有一个与之关联的 News 模型.
Users.php  Users模型
 1 <?php
 2 
 3 namespace App\Model\Eloquent\Admin;
 4 
 5 use Illuminate\Database\Eloquent\Model;
 6 
 7 class Users extends Model
 8 {
 9     protected $table = 'users';
10 
11     /**
12      * 关联news表
13      */
14     public function newsMethod()
15     {
16         //hasOne($related, $foreignKey = null, $localKey = null)
17         //第一个参数为对应的model, 第二个参数默认为model对应的表的外键, 第三个参数默认为当前模型对应的表的主键
18         return $this->hasOne('App\Model\Eloquent\Admin\News','users_id', 'id');
19     }
20 }

 

News.php  News模型
 1 <?php
 2 
 3 namespace App\Model\Eloquent\Admin;
 4 
 5 use Illuminate\Database\Eloquent\Model;
 6 
 7 class News extends Model
 8 {
 9     protected $table = 'news';
10 
11     /**
12      * 相对关联users表
13      */
14     public function usersMethod()
15     {
16         //belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null)
17         //第一个参数为model, 先讲第四个参数,默认为调用belongsTo()的方法名字, 第二个参数默认为第四个参数加上 '_id', 第三个参数默认为model的对应表的主键
18         return $this->belongsTo('App\Model\Eloquent\Admin\Users','users_id','id');
19     }
20 }

 

关联查询 -- 查询news表关联id=1,一条数据
1 $data = Users::find(1)->newsMethod->toArray();
结果打印:


相对关联查询 -- 查询users表关联id=2,一条数据
1 $data = News::find(2)->usersMethod->toArray();
结果打印:

 
一对多
一对多关联:是定义单个模型拥有多个其它模型的关联关系.
Users.php  Users模型
 1 <?php
 2 
 3 namespace App\Model\Eloquent\Admin;
 4 
 5 use Illuminate\Database\Eloquent\Model;
 6 
 7 class Users extends Model
 8 {
 9     protected $table = 'users';
10 
11     /**
12      * 关联news表 假设一对多
13      */
14     public function newsMethodMany()
15     {
16         return $this->hasMany('App\Model\Eloquent\Home\News','users_id', 'id');
17     }
18 }
关联查询 -- 查询users和news两表关联id=3,两条数据
1 $data = Users::find(3)->newsMethodMany->toArray();
结果打印:


查询存在的关联关系操作语法.
with:类似于 SQL 中的 left join   注:渴求加载查指定字段, id字段是必须列出
1 $data = News::with('usersMethod:id,name')->get()->toArray();

结果打印:

 
has:类似于 SQL 中的 inner join
1 $data = News::has('usersMethod')->get()->toArray();
whereHas:inner join 之后,可以补充查询条件
1 $data = News::whereHas('usersMethod', function ($query) {
2     $query->where('author', '=', 'Hong');
3 })->get()->toArray();
结果打印:


www.phpzy.comtrue/php/29663.htmlTechArticlelaravel5.6ORM 关联模型,一对一和一对多 Laravel5.6 关联模型的操作,主要是一对一,一对多,多对多等操作.下面示例主要解析前面两个操作用法比较常用.(操作和用法TP5类似) 将关联查询使用语...

相关文章

PHP之友评论

今天推荐