当前位置: 首页 > 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;
}
http://www.lryc.cn/news/352901.html

相关文章:

  • 每日力扣刷题day03(从零开始版)
  • 误差反向传播简介与实现
  • ATmega328P加硬件看门狗MAX824L看门狗
  • 【Redis】 String类型的内部编码与使用环境
  • HarmonyOS interface router scale pageTransition SlideEffect.Left ArkTS ArkUI
  • Go语言(Golang)的开发框架
  • Python入门第三课——Python 数据类型(详细)
  • html入门
  • 蓝桥杯杨辉三角
  • 【活动】开源与闭源大模型:探索未来趋势的双轨道路
  • 虚拟局域网(VLAN)
  • 内网穿透--Frp-简易型(速成)-上线
  • Python库之Scrapy的简介、安装、使用方法详细攻略
  • k8s配置pods滚动发布
  • C++vector的简单模拟实现
  • AWTK实现汽车仪表Cluster/DashBoard嵌入式GUI开发(七):快启
  • 基于springboot+vue的招聘信息管理系统
  • 使用STM32F103标准库实现自定义键盘
  • 面试八股之JVM篇3.5——垃圾回收——G1垃圾回收器
  • 解决LabVIEW通过OPC Server读取PLC地址时的错误180121602
  • npm,yarn,cnpm,tyarn,pnpm 安使用装配置镜像
  • 使用python将一段文本写入一个txt文件中且先格式化文件名
  • 前端 CSS 经典:元素倒影
  • ROS学习记录:用C++实现IMU航向锁定
  • 设计模式-策略模式-使用
  • WebSocket——相关介绍以及后端配置
  • 单片机设计注意事项
  • Ubuntu 如何根据NVIDIA显卡型号确定对应的显卡驱动版本并安装
  • 如何选择一款安全高效的数据自动同步工具?
  • 【linux】docker下nextcloud安装人脸识别插件