婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 淺談laravel5.5 belongsToMany自身的正確用法

淺談laravel5.5 belongsToMany自身的正確用法

熱門標簽:電銷招聘機器人 格陵蘭島地圖標注 天津營銷電話機器人加盟代理 地圖標注入哪個科目 福泉電話機器人 太原極信防封電銷卡 熱線電話機器人 南寧crm外呼系統(tǒng)平臺 事業(yè)單位如何百度地圖標注

場景

用戶之間相互關(guān)注,記錄這種關(guān)系的是followers表(follower_id 發(fā)起關(guān)注的人 followed_id被關(guān)注的人)

現(xiàn)在的多對多的關(guān)系就不再是傳統(tǒng)的三張表的關(guān)系了, 這種情況 多對多關(guān)系應(yīng)該怎么聲明呢?

分析

laravel或者其他框架多對多的關(guān)系 一般都是由Model1 Model2 Model1_Model2(聲明兩者關(guān)系的表)來組成,

但是上面的場景 卻是只有兩張表,這時候就要研究下官方文檔了; 當然是支持的

參考資料

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方法傳遞的參數(shù)是可以定制的 以達到個性化的需求,

第一個參數(shù)是 第二個Model

第二個參數(shù)是 關(guān)系表名

第三個參數(shù)是 第一個Model在關(guān)系表中的外鍵ID

第四個參數(shù)是 第二個Model在關(guān)系表中的外鍵ID

解決

經(jīng)過分析

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

2. 關(guān)系表名是 'followers'

 /**
  * 關(guān)注當前用戶的
  * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
  */
 public function followers()
 {
  return $this->belongsToMany(self::class, 'followers', 'followed_id','follower_id')->withTimestamps()
   ->withTimestamps();
 }

 /**
  * 被當前用戶關(guān)注的用戶
  */
 public function followed()
 {
  return $this->belongsToMany(self::class, 'followers', 'follower_id', 'followed_id');
 }

以上這篇淺談laravel5.5 belongsToMany自身的正確用法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 基于laravel belongsTo使用詳解

標簽:佳木斯 郴州 通化 阿克蘇 寶雞 金華 香港 自貢

巨人網(wǎng)絡(luò)通訊聲明:本文標題《淺談laravel5.5 belongsToMany自身的正確用法》,本文關(guān)鍵詞  淺談,laravel5.5,belongsToMany,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《淺談laravel5.5 belongsToMany自身的正確用法》相關(guān)的同類信息!
  • 本頁收集關(guān)于淺談laravel5.5 belongsToMany自身的正確用法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 杨浦区| 朝阳区| 高台县| 文山县| 会东县| 买车| 乐亭县| 电白县| 宁阳县| 黄大仙区| 射阳县| 和田市| 山阳县| 大悟县| 石渠县| 繁昌县| 禄丰县| 建昌县| 怀远县| 海伦市| 招远市| 新津县| 曲沃县| 武隆县| 运城市| 建平县| 辉南县| 株洲市| 泰州市| 肇州县| 新营市| 旬阳县| 永嘉县| 静宁县| 阿鲁科尔沁旗| 女性| 曲周县| 苏尼特左旗| 鄂尔多斯市| 阿克苏市| 安塞县|