浅谈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 shell命令合并图片的代码
Jun 23 PHP
PHP CURL获取cookies模拟登录的方法
Nov 04 PHP
php读取excel文件示例分享(更新修改excel)
Feb 27 PHP
去除php注释和去除空格函数分享
Mar 13 PHP
destoon切换城市后实现logo旁边显示地区名称的方法
Aug 21 PHP
PHP中调用SVN命令更新网站方法
Jan 07 PHP
php禁止某ip或ip地址段访问的方法
Feb 25 PHP
php使用ffmpeg向视频中添加文字字幕的实现方法
May 23 PHP
搜索附近的人PHP实现代码
Feb 11 PHP
PHP实现浏览器中直接输出图片的方法示例
Mar 14 PHP
PHP 爬取网页的主要方法
Jul 13 PHP
YII框架常用技巧总结
Apr 27 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
一个阿拉伯数字转中文数字的函数
2006/10/09 PHP
mysql limit查询优化分析
2008/11/12 PHP
PHP COOKIE设置为浏览器进程
2009/06/21 PHP
php设置编码格式的方法
2013/03/05 PHP
ThinkPHP3.1新特性之动态设置自动完成和自动验证示例
2014/06/19 PHP
php 利用socket发送GET,POST请求的实例代码
2020/07/04 PHP
javascript图像处理—仿射变换深度理解
2013/01/16 Javascript
兼容IE和Firefox火狐的上下、左右循环无间断滚动JS代码
2013/04/19 Javascript
javascript按位非运算符的使用方法
2013/11/14 Javascript
用js通过url传参把数据从一个页面传到另一个页面
2014/09/01 Javascript
JS+CSS模拟可以无刷新显示内容的留言板实例
2015/03/03 Javascript
JavaScript中扩展Array contains方法实例
2020/08/23 Javascript
深入了解JavaScript中的Symbol的使用方法
2015/07/28 Javascript
jQuery常用知识点总结以及平时封装常用函数
2016/02/23 Javascript
JS实现获取剪贴板内容的方法
2016/06/21 Javascript
微信小程序 合法域名校验出错详解及解决办法
2017/03/09 Javascript
node的process以及child_process模块学习笔记
2018/03/06 Javascript
vue-cli项目中使用echarts图表实例
2018/10/22 Javascript
Vue Cli 3项目使用融云IM实现聊天功能的方法
2019/04/19 Javascript
JavaScript canvas基于数组生成柱状图代码实例
2020/03/06 Javascript
es6函数之尾递归用法实例分析
2020/04/25 Javascript
[01:03:27]NAVI vs EG 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
jupyter修改文件名方式(TensorFlow)
2020/04/21 Python
PyQt5通过信号实现MVC的示例
2021/02/06 Python
大学生预备党员自我评价分享
2013/11/16 职场文书
单位单身证明范本
2014/01/11 职场文书
2014年电厂个人工作总结
2014/11/27 职场文书
借条格式范本
2015/05/25 职场文书
2015年暑期社会实践总结
2015/07/13 职场文书
运动员入场前导词
2015/07/20 职场文书
2016中秋节月饼促销广告语
2016/01/28 职场文书
《纸船和风筝》教学反思
2016/02/18 职场文书
先进个人事迹材料(2016推荐版)
2016/03/01 职场文书
2016大学生诚信考试承诺书
2016/03/25 职场文书
MySQL之高可用集群部署及故障切换实现
2021/04/22 MySQL
sql注入报错之注入原理实例解析
2022/06/10 MySQL