当前位置: 首页 > news >正文

hyperf 多对多关联模型

这里使用到三张表,一张是用户(users),一张是角色(roles),一张是用户角色关联表(users_roles),
首先创建用户模型、角色模型

php bin/hyperf.php gen:model users
php bin/hyperf.php gen:model roles

users模型

<?php
declare (strict_types=1);
namespace App\Model;use Hyperf\DbConnection\Model\Model;
/*** users模型 */
class users extends Model
{protected $connection = 'sso';protected $primaryKey = 'ID';//因为我表里的主键是大写的,所以这里我用大写,否则使用关联模型会出问题/*** The table associated with the model.** @var string*/protected $table = 'users';/*** The attributes that are mass assignable.** @var array*/protected $fillable = [];/*** The attributes that should be cast to native types.** @var array*/protected $casts = [];/*多对多建立关系*/public function roles(){return $this->belongsToMany(roles::class,'users_roles','userId','roleId');}
}

roles模型

<?phpdeclare (strict_types=1);
namespace App\Model;use Hyperf\DbConnection\Model\Model;
/*** roles模型*/
class roles extends Model
{protected $connection = 'sso';protected $primaryKey = 'ID';/*** The table associated with the model.** @var string*/protected $table = 'roles';/*** The attributes that are mass assignable.** @var array*/protected $fillable = [];/*** The attributes that should be cast to native types.** @var array*/protected $casts = [];public function users(){return $this->belongsToMany(users::class,'users_roles','roleId','userId');}
}
  • 1、其中users_roles是我关联表的表名

  • 2、belongsToMany方法中,第一个参数,参数的与之关联的表模型;第二个参数是两个表的关联表(中间表);第三个参数是定义此关联的模型在连接表里的外键名;第四个参数是另一个模型在连接表里的外键名;

分页数据

public function paginate()
{$data = Db::table('hlyun_sso_users')->paginate(10);return $data;
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 智能化让幼儿园管理更加规范
  • world machine学习笔记(3)
  • 方正畅享全媒体新闻采编系统 binary.do SQL注入漏洞复现
  • 智能家居6 -- 配置 ini文件优化设备添加
  • 【C++】详解AVL树——平衡二叉搜索树
  • 电商公司需不需要建数字档案室呢
  • 下一代Docker会让部署更丝滑吗
  • 说些什么好呢
  • 鸿蒙系统和安卓系统通过termux搭建Linux系统—Centos
  • 常见算法(3)
  • AI爆文写作:或许开放性的标题,才会更让人想点开了解答案
  • easy-es EsAutoConfiguration RestHighLevelClient 没有自动注入配置
  • 算法训练营第三十九天 | LeetCode 738 单调递增的数字、LeetCode 968 监控二叉树
  • 非整数倍数据位宽转换24to128
  • Meta发布Chameleon模型预览,挑战多模态AI前沿
  • (三)从jvm层面了解线程的启动和停止
  • 【css3】浏览器内核及其兼容性
  • 10个确保微服务与容器安全的最佳实践
  • 2017-09-12 前端日报
  • Angular数据绑定机制
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • Javascript编码规范
  • maya建模与骨骼动画快速实现人工鱼
  • MyEclipse 8.0 GA 搭建 Struts2 + Spring2 + Hibernate3 (测试)
  • mysql 数据库四种事务隔离级别
  • React系列之 Redux 架构模式
  • SAP云平台运行环境Cloud Foundry和Neo的区别
  • Spring Boot快速入门(一):Hello Spring Boot
  • tensorflow学习笔记3——MNIST应用篇
  • 代理模式
  • 将 Measurements 和 Units 应用到物理学
  • 力扣(LeetCode)965
  • 码农张的Bug人生 - 初来乍到
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 优秀架构师必须掌握的架构思维
  • 如何正确理解,内页权重高于首页?
  • #!/usr/bin/python与#!/usr/bin/env python的区别
  • #pragma once
  • $.each()与$(selector).each()
  • $redis-setphp_redis Set命令,php操作Redis Set函数介绍
  • (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)
  • (java版)排序算法----【冒泡,选择,插入,希尔,快速排序,归并排序,基数排序】超详细~~
  • (二)十分简易快速 自己训练样本 opencv级联lbp分类器 车牌识别
  • (二十一)devops持续集成开发——使用jenkins的Docker Pipeline插件完成docker项目的pipeline流水线发布
  • (附源码)springboot宠物管理系统 毕业设计 121654
  • (一)RocketMQ初步认识
  • (转)程序员疫苗:代码注入
  • (转)负载均衡,回话保持,cookie
  • (转载)(官方)UE4--图像编程----着色器开发
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地定义和使用弱事件
  • .net实现客户区延伸至至非客户区
  • .NET中使用Protobuffer 实现序列化和反序列化
  • .net中我喜欢的两种验证码
  • @for /l %i in (1,1,10) do md %i 批处理自动建立目录
  • @软考考生,这份软考高分攻略你须知道