1

I am using laravel .i have printed print_r($data->toArray()); in view it producing following array

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
                )

        )

)

i am trying to printout album_images data but its returning empty

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

if i print print_r($data->album_name); its returning result

both returning empty

Even i have printed in controller before returning to view

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

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

returning no result can any one help me where i am doing wrong

Controller

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

Updated

  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 => "*"
    ]
  }
}
14
  • 1
    How did you print the array in your view? if $data is an array, print_r($data['album_images']); should be the correct way to do it. Commented Feb 21, 2017 at 9:27
  • perhaps can you show the part of your code which handle that result Commented Feb 21, 2017 at 9:29
  • @Jerodev. if i print print_r($data->album_id); then it will display.only facing problem in print_r($data->album_images); Commented Feb 21, 2017 at 9:30
  • this is correct print_r($data['album_images']); it might not display the result because print_r($data->album_images); stops the programs when it error which it might not reach this code print_r($data['album_images']); Commented Feb 21, 2017 at 9:32
  • @Beginner print_r($data['album_images']); only i have printed no use Commented Feb 21, 2017 at 9:33

2 Answers 2

1

Why don't you first convert $data into an array $data = $data->toArray(); and then try to get the data out of it like so $data['album-images'].

Let me out, if that works for you.

Sign up to request clarification or add additional context in comments.

3 Comments

.thanks it works.but i have dobut why its not working if we are accessing directly
Using stdclass is sometimes a bit tricky, especially when it comes to multi-layer ones. Unless you know exactly what is does, it is better to use an array.
@Michal.Thanks for the info
0

I tried like this:

In controller:

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

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

In view:

{{print_r($user->album_images)}}

Please try like this and let me know what the result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.