Script execution / foreach() loop stops when I call Laravel's dd() function [duplicate]
Asked Answered
B

2

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?

Barney answered 23/12, 2015 at 4:0 Comment(1)
Topically related: How to display a readable array - LaravelCallipygian
S
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);
}
Schleicher answered 23/12, 2015 at 4:39 Comment(0)
E
-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

Expellee answered 23/12, 2015 at 4:9 Comment(7)
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); }Barney
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.Barney
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?Expellee
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().Expellee
Yes if you look at the original post I am basically getting that except without the collection part now. I just have an array with two users at 0 => user, and 1 => user. I looked at the collections and I can do $user->friends()->count() and I receive 2.Barney
So this is kind of a funny error I got with I did the all()->asArray . Call to a member function asArray() on array. But when I do the friends()->toArray it works just fine.Barney
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.Schleicher

© 2022 - 2024 — McMap. All rights reserved.