0

When I call dd() (die and dump) like dd($user->friends()); I only get the output from the first record in the collection.

When I try to do something like:

foreach($user->friends() as $friend) {
    dd($friend);
}

This is what I get :

User {#189 ▼
  #table: "users"
  #fillable: array:7 [▶]
  #hidden: array:3 [▶]
  #connection: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:11 [▶]
  #original: array:13 [▶]
  #relations: array:1 [▶]
  #visible: []
  #appends: []
  #guarded: array:1 [▶]
  #dates: []
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: true
  +wasRecentlyCreated: false
}

But when I dump out the collection without looping, I can see multiple records.

Collection {#184 ▼
  #items: array:2 [▼
    0 => User {#189 ▼
      #table: "users"
      #fillable: array:7 [▶]
      #hidden: array:3 [▶]
      #connection: null
      #primaryKey: "id"
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:11 [▶]
      #original: array:13 [▶]
      #relations: array:1 [▶]
      #visible: []
      #appends: []
      #guarded: array:1 [▶]
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
    1 => User {#190 ▼
      #table: "users"
      #fillable: array:7 [▶]
      #hidden: array:3 [▶]
      #connection: null
      #primaryKey: "id"
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:11 [▶]
      #original: array:13 [▶]
      #relations: array:1 [▶]
      #visible: []
      #appends: []
      #guarded: array:1 [▶]
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
  ]
}

I want it to loop through all the users not just the first one. Is there a reason it is doing this. Am I doing the foreach wrong or does it have something to do with the collection?

1

2 Answers 2

8

When you do your foreach, you're only seeing one entry because of the dd(). Remember, it is "dump and die", so on the very first iteration, you're dumping the record and then dying.

Try this out:

foreach($user->friends() as $friend) {
    dump($friend);
}
Sign up to request clarification or add additional context in comments.

Comments

-1

If you just want to treat it as an array, use toArray on the collection first. Eg:

$friends = $user->friends()->toArray();
foreach($friends as $friend){
 ...some stuff...
}

Otherwise, use the features of the laravel collection as per the docs here: http://laravel.com/docs/5.2/collections

7 Comments

That pushed it to an array , but for some reason if I do that I still only get one user back. $friends = $user->friends()->toArray(); foreach($friends as $friend){ dd($friend); }
I tried to make that look nice sorry it does not , I basically did what you said and inside the foreach dd($friend) and still am getting one user back, but I am getting an array back now.
Try it this way instead and see if it works. $friends = $user->friends->toArray(); Otherwise, are you sure you have more than 1 friend associated with the user?
Oh, duh.....sorry, I'm a bit rusty with Laravel. Ignore my previous comment. I think this is what you need: $user->friends->all()->asArray().
None of this toArray() stuff is needed. Collections are traversable, so they work in foreach natively. The issue is that dd() is "dump and die", emphasis on the "die" part. You'll hit the first iteration, and then die.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.