Php 从Laravel中的pivotParent获取数据

Php 从Laravel中的pivotParent获取数据,php,laravel-5,Php,Laravel 5,目前我在MySQLvideos中有3个表:关键字、视频_关键字。video_keywords是一个透视表单videos和keywords id、keywords_id、video_id。主要问题是:如何从keywords表接收来自pivotParentname表的数据?目前我只能从视频关键字表中获取关键字id、视频id以及视频表中的所有数据,但无法从关键字表中获取任何内容 在keywords/list.blade.php中转储$keyword->videos将提供输出: [{id:4,youtu

目前我在MySQLvideos中有3个表:关键字、视频_关键字。video_keywords是一个透视表单videos和keywords id、keywords_id、video_id。主要问题是:如何从keywords表接收来自pivotParentname表的数据?目前我只能从视频关键字表中获取关键字id、视频id以及视频表中的所有数据,但无法从关键字表中获取任何内容

在keywords/list.blade.php中转储$keyword->videos将提供输出:

[{id:4,youtube\u id:cq2Ef6rvL6g,标题:环境放松休息室放松音乐-基本放松课程1-背景放松音乐-创建时间:2020-01-22 14:39:41,更新时间:2020-01-22 14:39:41,轴心:{关键字\u id:5,视频\u id:4}]

BelongToMany返回一个Illumb\Database\Eloquent\Collection实例,它是关键字视频的列表

因此,您可以检索第一个视频的名称,如下所示:

转储$keyword->videos->first->name; 或者,如果要循环浏览视频列表,请执行以下操作:

foreach$关键字->视频作为$video{ 转储$video->name; } 您可以在此处阅读更多关于多对多关系的信息:

  #items: array:1 [▼
    0 => App\Models\Video {#390 ▼
      #fillable: array:2 [▶]
      #connection: "mysql"
      #table: "videos"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:5 [▶]
      #original: array:7 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "pivot" => Illuminate\Database\Eloquent\Relations\Pivot {#391 ▼
          #guarded: []
          #connection: "mysql"
          #table: "video_keywords"
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:2 [▶]
          #original: array:2 [▶]
          #changes: []
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: false
          #hidden: []
          #visible: []
          #fillable: []
          +pivotParent: App\Models\Keyword {#356 ▼
            #fillable: array:1 [▶]
            #connection: "mysql"
            #table: "keywords"
            #primaryKey: "id"
            #keyType: "int"
            +incrementing: true
            #with: []
            #withCount: []
            #perPage: 15
            +exists: true
            +wasRecentlyCreated: false
            #attributes: array:4 [▼
              "id" => "5"
              "name" => "chill out music"
              "created_at" => "2020-01-22 14:40:52"
              "updated_at" => "2020-01-22 14:40:52"
            ]
            #original: array:4 [▶]
            #changes: []
            #casts: []
            #dates: []
            #dateFormat: null
            #appends: []
            #dispatchesEvents: []
            #observables: []
            #relations: array:1 [▶]
            #touches: []
            +timestamps: true
            #hidden: []
            #visible: []
            #guarded: array:1 [▶]
          }
          #foreignKey: "keyword_id"
          #relatedKey: "video_id"
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }
  ]
}```