2017-02-21 108 views
1

我使用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-> 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 => "*" 
    ] 
    } 
} 
+1

您是如何在視圖中打印數組的?如果'$ data'是一個數組,'print_r($ data ['album_images']);'應該是正確的方法。 – Jerodev

+0

也許你可以顯示處理結果的部分代碼 – Beginner

+0

@Jerodev。如果我打印print_r($ data-> album_id);那麼它會顯示。只在print_r($ data-> album_images)中遇到問題; – iCoders

回答

1

你爲什麼不先轉換$data到一個數組$data = $data->toArray();,然後嘗試獲取數據,它像這樣$data['album-images']

讓我出去,如果這對你有用。

+0

。感謝它的工作。但我有dobut但爲什麼它不工作,如果我們直接訪問 – iCoders

+0

使用stdclass有時有點棘手,尤其是涉及到多層的時候。除非你確切地知道是什麼,否則最好使用一個數組。 –

+0

@ Michal.Thanks的信息 – iCoders

0

我試圖像這樣:

控制器

$數據=相冊::其中( '編號',$ ID) - >用( 'AlbumImages') - >第一() ;

return View:make('new',compact('data'));

視圖

{{的print_r($用戶> album_images)}}

請嘗試像這樣,讓我知道什麼樣的結果。