浅谈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 相关文章推荐
深入phpMyAdmin的安装与配置的详细步骤
May 07 PHP
如何利用PHP执行.SQL文件
Jul 05 PHP
神盾加密解密教程(三)PHP 神盾解密工具
Jun 08 PHP
Linux下PHP安装mcrypt扩展模块笔记
Sep 10 PHP
PHP操作文件的一些基本函数使用示例
Nov 18 PHP
PHP生成RSS文件类实例
Dec 05 PHP
php中使用base HTTP验证的方法
Apr 20 PHP
Laravel框架使用Seeder实现自动填充数据功能
Jun 13 PHP
php接口隔离原则实例分析
Nov 11 PHP
PHP call_user_func和call_user_func_array函数的简单理解与应用分析
Nov 25 PHP
php的无刷新操作实现方法分析
Feb 28 PHP
PHP正则之正向预查与反向预查讲解与实例
Apr 06 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生成excel文件的简单方法
2014/02/08 PHP
Smarty foreach控制循环次数的一些方法
2015/07/01 PHP
PHP加密解密函数详解
2015/10/28 PHP
在openSUSE42.1下编译安装PHP7 的方法
2015/12/24 PHP
yii框架搜索分页modle写法
2016/12/19 PHP
Firefox中beforeunload事件的实现缺陷浅析
2012/05/03 Javascript
利用Jquery实现可多选的下拉框
2014/02/21 Javascript
javascript学习笔记(三)BOM和DOM详解
2014/09/30 Javascript
javascript实现点击提交按钮后显示loading的方法
2015/07/03 Javascript
jQuery实现带延迟的二级tab切换下拉列表效果
2015/09/01 Javascript
jQuery实现的仿百度分页足迹效果代码
2015/10/30 Javascript
JavaScript获取对象在页面中位置坐标的方法
2016/02/03 Javascript
Bootstrap+jfinal退出系统弹出确认框的实现方法
2016/05/30 Javascript
javascript表单控件实例讲解
2016/09/13 Javascript
JavaScript基本类型值-Undefined、Null、Boolean
2017/02/23 Javascript
JS实现导出Excel的五种方法详解【附源码下载】
2018/03/15 Javascript
利用Node.js批量抓取高清妹子图片实例教程
2018/08/02 Javascript
详解在vue-cli中使用graphql即vue-apollo的用法
2018/09/08 Javascript
vue-cli在 history模式下的配置详解
2019/11/26 Javascript
Node.js API详解之 Error模块用法实例分析
2020/05/14 Javascript
谈谈JavaScript中的垃圾回收机制
2020/09/17 Javascript
python和shell实现的校验IP地址合法性脚本分享
2014/10/23 Python
python学习数据结构实例代码
2015/05/11 Python
用python处理图片实现图像中的像素访问
2018/05/04 Python
Python3实现获取图片文字里中文的方法分析
2018/12/13 Python
Python读写文件模式和文件对象方法实例详解
2019/09/17 Python
Django实现简单网页弹出警告代码
2019/11/15 Python
逼真的HTML5树叶飘落动画
2016/03/01 HTML / CSS
html5实现移动端适配完美写法
2017/11/16 HTML / CSS
什么是servlet
2012/05/08 面试题
应付会计岗位职责
2013/12/12 职场文书
毕业生个人投资创业计划书
2014/01/04 职场文书
商场活动策划方案
2014/01/24 职场文书
商业项目策划方案
2014/06/05 职场文书
活动总结模板大全
2015/05/11 职场文书
活动经费申请报告
2015/05/15 职场文书