Home:ALL Converter>Laravel collections iteration with foreach loop

Laravel collections iteration with foreach loop

Ask Time:2018-06-08T22:12:34         Author:mhk

Json Formatter

order model product model

its a simple query in a controller using Order model:

$orders = Order::where('user_id', auth()->user()->id)
                        ->orderBy('created_at', 'desc')
                        ->get();
return dd($orders);

dd gives following results: (which is correct)

Collection {#269 ▼
  #items: array:2 [▼
    0 => Order {#270 ▼
      #dates: array:1 [▶]
      #fillable: array:6 [▶]
      #connection: "mysql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:9 [▶]
      #original: array:9 [▶]
      #changes: []
      #casts: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
      #forceDeleting: false
    }
    1 => Order {#271 ▼
      #dates: array:1 [▶]
      #fillable: array:6 [▶]
      #connection: "mysql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:9 [▶]
      #original: array:9 [▶]
      #changes: []
      #casts: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
      #forceDeleting: false
    }
  ]
}

but when i iterate through the $orders collection foreach loop, only the first array is is shown, the second array is not accessible... whats wrong with foreach loop... ?

$temp='';
        foreach($orders as $order){
                $temp.= $order->product; // accessing the belongTo method
        }

        dd($temp);

here is the output (only one array is shown):

"{"id":13,"created_at":"2018-06-06 15:28:21","updated_at":"2018-06-06 18:36:28","type":0,"title":"product 3","description":"this is product no 3 with 5 images","images":"night-product-watch-dramatic-84475_1528310188.jpeg,night-product-watch-dramatic-84475_1528310188.jpeg","alive":1,"user_id":1,"deleted_at":null} ◀"

Author:mhk,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50762576/laravel-collections-iteration-with-foreach-loop
Leon Vismer :

If you are building an array of products across all orders you cannot use string concatenation. Use an array and push items onto it.\n\n$products = [];\nforeach ($orders as $order) {\n if ($order->product) {\n $products[] = $order->product;\n }\n}\ndd($products); // will be an array of all other products.\n",
2018-06-08T14:16:55
yy