浅谈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面向对象全攻略 (五) 封装性
Sep 30 PHP
php笔记之:php数组相关函数的使用
Apr 26 PHP
使用Smarty 获取当前日期时间和格式化日期时间的方法详解
Jun 18 PHP
PHP中CURL的CURLOPT_POSTFIELDS参数使用细节
Mar 17 PHP
php使用curl存储cookie的示例
Mar 31 PHP
强制PHP命令行脚本单进程运行的方法
Apr 15 PHP
linux下实现定时执行php脚本
Feb 13 PHP
PHP MYSQL实现登陆和模糊查询两大功能
Feb 05 PHP
PHP实现文件上传与下载实例与总结
Mar 13 PHP
PHP 以POST方式提交XML、获取XML,解析XML详解及实例
Oct 26 PHP
PHP进制转换实例分析(2,8,16,36,64进制至10进制相互转换)
Feb 04 PHP
THINKPHP5.1 Config的配置与获取详解
Jun 08 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
php下清空字符串中的HTML标签的代码
2010/09/06 PHP
php 随机排序广告的实现代码
2011/05/09 PHP
destoon官方标签大全
2014/06/20 PHP
PHP中文竖排转换实现方法
2015/10/23 PHP
php实现对文件压缩简单的方法
2019/09/29 PHP
判断JavaScript对象是否可用的最正确方法分析
2008/10/03 Javascript
js通过googleAIP翻译PHP系统的语言配置的实现代码
2011/10/17 Javascript
JS获取节点的兄弟,父级,子级元素的方法
2014/01/09 Javascript
iframe实用操作锦集
2014/04/22 Javascript
JQuery对表单元素的基本操作使用总结
2014/07/18 Javascript
深入解读JavaScript中的Hoisting机制
2015/08/12 Javascript
javascript实现获取浏览器版本、浏览器类型
2015/12/02 Javascript
webpack+vue.js实现组件化详解
2016/10/12 Javascript
js获取时间函数及扩展函数的方法
2016/10/30 Javascript
jQuery Masonry瀑布流布局神器使用详解
2017/05/25 jQuery
JS设计模式之状态模式概念与用法分析
2018/02/05 Javascript
React实现全局组件的Toast轻提示效果
2018/09/21 Javascript
Angularjs实现数组随机排序的方法
2018/10/02 Javascript
微信公众号H5支付接口调用方法
2019/01/10 Javascript
基于jquery ajax的多文件上传进度条过程解析
2019/09/11 jQuery
微信小程序select下拉框实现源码
2019/11/08 Javascript
python中 chr unichr ord函数的实例详解
2017/08/06 Python
python读取word文档,插入mysql数据库的示例代码
2018/11/07 Python
python带参数打包exe及调用方式
2019/12/21 Python
python2 对excel表格操作完整示例
2020/02/23 Python
python爬虫基础知识点整理
2020/06/02 Python
利用Python实现朋友圈中的九宫格图片效果
2020/09/03 Python
详解基于Scrapy的IP代理池搭建
2020/09/29 Python
Python实现粒子群算法的示例
2021/02/14 Python
Backcountry旗下的户外商品闪购网站:steep&cheap
2016/09/22 全球购物
中软Java笔试题
2012/11/11 面试题
大学生毕业鉴定
2014/01/31 职场文书
完美的中文自荐信
2014/05/24 职场文书
国庆65周年演讲稿:回首往昔,展望未来
2014/09/21 职场文书
2015年世界无烟日演讲稿
2015/03/18 职场文书
Python 读写 Matlab Mat 格式数据的操作
2021/05/19 Python