浅谈laravel5.5 belongsToMany自身的正确用法


Posted in PHP onOctober 17, 2019

场景

用户之间相互关注,记录这种关系的是followers表(follower_id 发起关注的人 followed_id被关注的人)

现在的多对多的关系就不再是传统的三张表的关系了, 这种情况 多对多关系应该怎么声明呢?

分析

laravel或者其他框架多对多的关系 一般都是由Model1 Model2 Model1_Model2(声明两者关系的表)来组成,

但是上面的场景 却是只有两张表,这时候就要研究下官方文档了; 当然是支持的

参考资料

https://laravel.com/docs/5.6/eloquent-relationships#many-to-many

In addition to customizing the name of the joining table, you may also customize the column names of the keys on the table by passing additional arguments to the belongsToMany method. The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to:

belongsToMany方法传递的参数是可以定制的 以达到个性化的需求,

第一个参数是 第二个Model

第二个参数是 关系表名

第三个参数是 第一个Model在关系表中的外键ID

第四个参数是 第二个Model在关系表中的外键ID

解决

经过分析

1. 第一个Model是User 第一个Model也是User

2. 关系表名是 'followers'

/**
  * 关注当前用户的
  * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
  */
 public function followers()
 {
  return $this->belongsToMany(self::class, 'followers', 'followed_id','follower_id')->withTimestamps()
   ->withTimestamps();
 }

 /**
  * 被当前用户关注的用户
  */
 public function followed()
 {
  return $this->belongsToMany(self::class, 'followers', 'follower_id', 'followed_id');
 }

以上这篇浅谈laravel5.5 belongsToMany自身的正确用法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
PHP游戏编程25个脚本代码
Feb 08 PHP
php在页面中调用fckeditor编辑器的方法
Jun 10 PHP
PHP判断数据库中的记录是否存在的方法
Nov 14 PHP
php实现的树形结构数据存取类实例
Nov 29 PHP
服务器上配置PHP运行环境教程
Feb 12 PHP
PHP实现上传文件并存进数据库的方法
Jul 16 PHP
php Session无效分析资料整理
Nov 29 PHP
php使用PDO获取结果集的方法
Feb 16 PHP
PHP和MYSQL实现分页导航思路详解
Apr 11 PHP
PHP实现的下载远程文件类定义与用法示例
Jul 05 PHP
Ubuntu上安装yaf扩展的方法
Jan 29 PHP
使用laravel和ECharts实现折线图效果的例子
Oct 09 PHP
解决laravel5.4下的group by报错的问题
Oct 16 #PHP
laravel ORM关联关系中的 with和whereHas用法
Oct 16 #PHP
laravel 模型查询按照whereIn排序的示例
Oct 16 #PHP
解决Laravel无法使用COOKIE和SESSION的问题
Oct 16 #PHP
laravel 使用事件系统统计浏览量的实现
Oct 16 #PHP
关于laravel 子查询 & join的使用
Oct 16 #PHP
laravel高级的Join语法详解以及使用Join多个条件
Oct 16 #PHP
You might like
windows中为php安装mongodb与memcache
2015/01/06 PHP
Zend Framework框架教程之Zend_Db_Table_Rowset用法实例分析
2016/03/21 PHP
PHP htmlspecialchars()函数用法与实例讲解
2019/03/08 PHP
PHP语言对接抖音快手小红书视频/图片去水印API接口源码
2020/08/11 PHP
各浏览器中querySelector和querySelectorAll的实现差异分析
2012/05/23 Javascript
商城常用滚动的焦点图效果代码简单实用
2013/03/28 Javascript
浅析JQuery获取和设置Select选项的常用方法总结
2013/07/04 Javascript
讨论html与javascript在浏览器中的加载顺序问题
2013/11/27 Javascript
js的hasownproperty使用示例
2014/03/02 Javascript
超实用的JavaScript表单代码段
2016/02/26 Javascript
js日期插件dateHelp获取本月、三个月、今年的日期
2016/03/07 Javascript
jQuery代码性能优化的10种方法
2016/06/21 Javascript
JavaScript实现解析INI文件内容的方法
2016/11/17 Javascript
jquery Easyui Datagrid实现批量操作(编辑,删除,添加)
2017/02/20 Javascript
BootStrap 动态表单效果
2017/06/02 Javascript
jQuery中将json数据显示到页面表格的方法
2018/05/27 jQuery
Node.js中读取TXT文件内容fs.readFile()用法
2018/10/10 Javascript
Vue用v-for给循环标签自身属性添加属性值的方法
2018/10/18 Javascript
优雅的elementUI table单元格可编辑实现方法详解
2018/12/23 Javascript
[01:01:24]LGD vs Fnatic 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
Python简单获取自身外网IP的方法
2016/09/18 Python
python利用rsa库做公钥解密的方法教程
2017/12/10 Python
linux查找当前python解释器的位置方法
2019/02/20 Python
Win10系统下安装labelme及json文件批量转化方法
2019/07/30 Python
调用其他python脚本文件里面的类和方法过程解析
2019/11/15 Python
tensorflow 获取checkpoint中的变量列表实例
2020/02/11 Python
python多项式拟合之np.polyfit 和 np.polyld详解
2020/02/18 Python
python shapely.geometry.polygon任意两个四边形的IOU计算实例
2020/04/12 Python
Boston Proper官网:美国女装品牌
2017/10/30 全球购物
英国户外玩具儿童游乐设备网站:TP Toys(蹦床、攀爬框架、秋千、滑梯和游戏屋)
2018/04/09 全球购物
俄罗斯香水和化妆品购物网站:Л’Этуаль
2018/05/10 全球购物
2014年高中生自我评价范文
2014/09/26 职场文书
失恋33天观后感
2015/06/11 职场文书
大学生入党自传2015
2015/06/26 职场文书
2015年工会工作总结范文
2015/07/23 职场文书
MySQL中LAG()函数和LEAD()函数的使用
2022/08/14 MySQL