您现在的位置是:首页 > 经验记录>Laravel日记>laravel belongsTo和hasOne连接不同数据库的表 网站首页 Laravel日记
laravel belongsTo和hasOne连接不同数据库的表
protected $table = '要连的表名A'; //protected $table = '要连的表名C'; public function getA(){ //如果连不同库的表 return $this ->setConnection('config中database.php中的A表所在库的连接名') ->hasOne('\App\Components\控制器同名\Models\XXXX(要连表的模型名A)', 'id', 'b_id'); //如果连相同库的表 return $this ->hasOne('\App\Components\控制器同名\Models\XXXX(要连表的模型名A)', 'id', 'b_id');} public function getC(){ //如果连不同库的表 return $this ->setConnection('config中database.php中的A表所在库的连接名') ->belongsTo('\App\Components\控制器同名\Models\XXXX(要连表的模型名C)', 'id', 'c_id'); //如果连相同库的表 return $this ->belongsTo('\App\Components\控制器同名\Models\XXXX(要连表的模型名C)', 'id', 'c_id');} 转至:https://blog.csdn.net/tclzsn7456/article/details/86581359
随机推荐
- Mysql允许远程链接(直接使用navicat走非ssh通道连接)的方法
- windows phpStudy ThinkPHP 5.1 Workerman 快速上手指南
- 系统学习——JavaScript
- ubuntu 下搭建redis和php的redis的拓展
- ceph三节点基本服务搭建
- 速度太慢或直接失联?Golang 如何设置为国内镜像
- go-colly 使用文档【翻译】
- 在 Laravel 中使用 基于Workerman 的Gateway-worker进行 socket 长连接通讯
- 阿里云 Failed to download metadata for repo ‘AppStream‘(centos8放弃维护)
- Ubuntu18.04 lnmp nginx1.14 PHP7.2 MySQL5.7 安装 配置
文章评论
未开放