无法访问数组数组

时间:2017-02-21 09:26:17

标签: php arrays laravel laravel-5 laravel-5.3

我正在使用laravel .i已经打印print_r($data->toArray());,因为它产生了以下数组

Array
(
    [id] => 1
    [album_name] => asfasfsaf
    [album_release_date] => 2017-02-08
    [album_produced_by] => safasfasfas
    [music_director_name] => fasfasfsafas
    [music_singer_name] => fasfas
    [album_featured_image] => 3.JPG
    [album_description] => sfsa
    [created_at] => 2017-02-08 05:39:10
    [updated_at] => 2017-02-08 05:39:10
    [album_images] => Array
        (
            [0] => Array
                (
                    [id] => 20
                    [album_id] => 1
                    [album_image_name] => 1486549223.JPG
                    [album_image_description] => 
                    [created_at] => 2017-02-08 10:20:23
                    [updated_at] => 2017-02-08 10:20:23
                )

            [1] => Array
                (
                    [id] => 21
                    [album_id] => 1
                    [album_image_name] => dsgsdds
                    [album_image_description] => sdgsdg
                    [created_at] => 2017-02-14 00:00:00
                    [updated_at] => 2017-02-21 00:00:00
                )

            [2] => Array
                (
                    [id] => 22
                    [album_id] => 1
                    [album_image_name] => fasf.jpg
                    [album_image_description] => safsaf
                    [created_at] => 2017-02-21 00:00:00
                    [updated_at] => 2017-02-21 00:00:00
                )

        )

)

我正在尝试打印输出album_images数据但其返回空

print_r($data->album_images);
print_r($data['album_images']);

如果我打印print_r($data->album_name);其返回结果

都返回空

即使我在返回视图之前已在控制器中打印

$data=  Album::where('id', $id)->with('AlbumImages')->first();

echo "<pre>";
  print_r($data->toArray());
  print_r($data->album_images);

没有结果 任何人都可以帮助我在哪里做错了

控制器

  $data=  Album::where('id', $id)->with('AlbumImages')->first();

更新

  Album {#316 ▼
      #table: "albums"
      #connection: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:10 [▼
        "id" => 1
        "album_name" => "asfasfsaf"
        "album_release_date" => "2017-02-08"
        "album_produced_by" => "safasfasfas"
        "music_director_name" => "fasfasfsafas"
        "music_singer_name" => "fasfas"
        "album_featured_image" => "3.JPG"
        "album_description" => "sfsa"
        "created_at" => "2017-02-08 05:39:10"
        "updated_at" => "2017-02-08 05:39:10"
      ]
      #original: array:10 [▼
        "id" => 1
        "album_name" => "asfasfsaf"
        "album_release_date" => "2017-02-08"
        "album_produced_by" => "safasfasfas"
        "music_director_name" => "fasfasfsafas"
        "music_singer_name" => "fasfas"
        "album_featured_image" => "3.JPG"
        "album_description" => "sfsa"
        "created_at" => "2017-02-08 05:39:10"
        "updated_at" => "2017-02-08 05:39:10"
      ]
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #events: []
      #observables: []
      #relations: array:1 [▼
        "AlbumImages" => Collection {#320 ▼
          #items: array:3 [▼
            0 => AlbumGallery {#324 ▼
              #table: "album_gallerys"
              #connection: null
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:6 [▶]
              #original: array:6 [▼
                "id" => 20
                "album_id" => 1
                "album_image_name" => "1486549223.JPG"
                "album_image_description" => null
                "created_at" => "2017-02-08 10:20:23"
                "updated_at" => "2017-02-08 10:20:23"
              ]
              #casts: []
              #dates: []
              #dateFormat: null
              #appends: []
              #events: []
              #observables: []
              #relations: []
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #fillable: []
              #guarded: array:1 [▶]
            }
            1 => AlbumGallery {#325 ▼
              #table: "album_gallerys"
              #connection: null
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:6 [▼
                "id" => 21
                "album_id" => 1
                "album_image_name" => "dsgsdds"
                "album_image_description" => "sdgsdg"
                "created_at" => "2017-02-14 00:00:00"
                "updated_at" => "2017-02-21 00:00:00"
              ]
              #original: array:6 [▶]
              #casts: []
              #dates: []
              #dateFormat: null
              #appends: []
              #events: []
              #observables: []
              #relations: []
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #fillable: []
              #guarded: array:1 [▶]
            }
            2 => AlbumGallery {#326 ▶}
          ]
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]
    }



**Updated 2**
  

DD($ DATA-&GT; relationship_method());

HasMany {#312 ▼
  #foreignKey: "album_gallerys.album_id"
  #localKey: "id"
  #query: Builder {#315 ▶}
  #parent: Album {#316 ▼
    #table: "albums"
    #connection: null
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #perPage: 15
    +exists: true
    +wasRecentlyCreated: false
    #attributes: array:10 [▶]
    #original: array:10 [▼
      "id" => 1
      "album_name" => "asfasfsaf"
      "album_release_date" => "2017-02-08"
      "album_produced_by" => "safasfasfas"
      "music_director_name" => "fasfasfsafas"
      "music_singer_name" => "fasfas"
      "album_featured_image" => "3.JPG"
      "album_description" => "sfsa"
      "created_at" => "2017-02-08 05:39:10"
      "updated_at" => "2017-02-08 05:39:10"
    ]
    #casts: []
    #dates: []
    #dateFormat: null
    #appends: []
    #events: []
    #observables: []
    #relations: array:1 [▶]
    #touches: []
    +timestamps: true
    #hidden: []
    #visible: []
    #fillable: []
    #guarded: array:1 [▼
      0 => "*"
    ]
  }
  #related: AlbumGallery {#298 ▼
    #table: "album_gallerys"
    #connection: null
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #perPage: 15
    +exists: false
    +wasRecentlyCreated: false
    #attributes: []
    #original: []
    #casts: []
    #dates: []
    #dateFormat: null
    #appends: []
    #events: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #hidden: []
    #visible: []
    #fillable: []
    #guarded: array:1 [▼
      0 => "*"
    ]
  }
}

2 个答案:

答案 0 :(得分:1)

为什么不首先将$data转换为数组$data = $data->toArray();,然后尝试从中$data['album-images']获取数据。

如果那对您有用,请告诉我。

答案 1 :(得分:0)

我试过这样:

控制器

$ data =专辑:: where(&#39; id&#39;,$ id) - &gt; with(&#39; AlbumImages&#39;) - &gt; first();

返回View :: make(&#39; new&#39;,compact(&#39; data&#39;));

视图

{{的print_r($用户&GT; album_images)}}

请尝试这样,让我知道结果。