eddy1992's avatar

How to loop through collection ?

How would i loop through this collection ? Please help.

Illuminate\Database\Eloquent\Collection Object
(
    [items:protected] => Array
        (
            [0] => App\Order Object
                (
                    [fillable:protected] => Array
                        (
                            [0] => store_id
                            [1] => address_id
                            [2] => customer_id
                            [3] => token
                            [4] => order_total
                            [5] => payment_type_id
                            [6] => shipping_address
                            [7] => billing_address
                            [8] => order_status
                        )

                    [connection:protected] => 
                    [table:protected] => 
                    [primaryKey:protected] => id
                    [perPage:protected] => 15
                    [incrementing] => 1
                    [timestamps] => 1
                    [attributes:protected] => Array
                        (
                            [id] => 27
                            [customer_id] => 8
                            [store_id] => 1
                            [address_id] => 0
                            [token] => OT5SdfJFWA
                            [order_total] => 0.00
                            [payment_type_id] => 0
                            [shipping_address] => {"id":"8","customer_id":"8","name":"Aditya Banerjee","pincode":"110018","address":"H-5c Gulmohar Apartments Vikas Puri                                             \r\n                                        ","landmark":"New Delhi","city":"New delhi ","area":"New Delhi","state":null,"phone":"9958537480","created_at":"2016-01-04 10:55:01","updated_at":"2016-01-04 10:55:01"}
                            [billing_address] => {"id":"8","customer_id":"8","name":"Aditya Banerjee","pincode":"110018","address":"H-5c Gulmohar Apartments Vikas Puri                                             \r\n                                        ","landmark":"New Delhi","city":"New delhi ","area":"New Delhi","state":null,"phone":"9958537480","created_at":"2016-01-04 10:55:01","updated_at":"2016-01-04 10:55:01"}
                            [order_status] => 1
                            [created_at] => 2016-01-04 11:48:49
                            [updated_at] => 2016-01-04 11:48:56
                        )

                    [original:protected] => Array
                        (
                            [id] => 27
                            [customer_id] => 8
                            [store_id] => 1
                            [address_id] => 0
                            [token] => OT5SdfJFWA
                            [order_total] => 0.00
                            [payment_type_id] => 0
                            [shipping_address] => {"id":"8","customer_id":"8","name":"Aditya Banerjee","pincode":"110018","address":"H-5c Gulmohar Apartments Vikas Puri                                             \r\n                                        ","landmark":"New Delhi","city":"New delhi ","area":"New Delhi","state":null,"phone":"9958537480","created_at":"2016-01-04 10:55:01","updated_at":"2016-01-04 10:55:01"}
                            [billing_address] => {"id":"8","customer_id":"8","name":"Aditya Banerjee","pincode":"110018","address":"H-5c Gulmohar Apartments Vikas Puri                                             \r\n                                        ","landmark":"New Delhi","city":"New delhi ","area":"New Delhi","state":null,"phone":"9958537480","created_at":"2016-01-04 10:55:01","updated_at":"2016-01-04 10:55:01"}
                            [order_status] => 1
                            [created_at] => 2016-01-04 11:48:49
                            [updated_at] => 2016-01-04 11:48:56
                        )

                    [relations:protected] => Array
                        (
                            [orderDetails] => Illuminate\Database\Eloquent\Collection Object
                                (
                                    [items:protected] => Array
                                        (
                                            [0] => App\OrderDetail Object
                                                (
                                                    [fillable:protected] => Array
                                                        (
                                                            [0] => order_id
                                                            [1] => product_id
                                                            [2] => product_code
                                                            [3] => product_name
                                                            [4] => quantity
                                                            [5] => price
                                                        )

                                                    [connection:protected] => 
                                                    [table:protected] => 
                                                    [primaryKey:protected] => id
                                                    [perPage:protected] => 15
                                                    [incrementing] => 1
                                                    [timestamps] => 1
                                                    [attributes:protected] => Array
                                                        (
                                                            [id] => 54
                                                            [order_id] => 27
                                                            [product_id] => 9
                                                            [product_code] => 61
                                                            [product_name] => Gouda Cheese-200 gms
                                                            [quantity] => 1
                                                            [price] => 298
                                                            [created_at] => 2016-01-04 11:48:49
                                                            [updated_at] => 2016-01-04 11:48:49
                                                        )

                                                    [original:protected] => Array
                                                        (
                                                            [id] => 54
                                                            [order_id] => 27
                                                            [product_id] => 9
                                                            [product_code] => 61
                                                            [product_name] => Gouda Cheese-200 gms
                                                            [quantity] => 1
                                                            [price] => 298
                                                            [created_at] => 2016-01-04 11:48:49
                                                            [updated_at] => 2016-01-04 11:48:49
                                                        )

                                                    [relations:protected] => Array
                                                        (
                                                        )

                                                    [hidden:protected] => Array
                                                        (
                                                        )

                                                    [visible:protected] => Array
                                                        (
                                                        )

                                                    [appends:protected] => Array
                                                        (
                                                        )

                                                    [guarded:protected] => Array
                                                        (
                                                            [0] => *
                                                        )

                                                    [dates:protected] => Array
                                                        (
                                                        )

                                                    [dateFormat:protected] => 
                                                    [casts:protected] => Array
                                                        (
                                                        )

                                                    [touches:protected] => Array
                                                        (
                                                        )

                                                    [observables:protected] => Array
                                                        (
                                                        )

                                                    [with:protected] => Array
                                                        (
                                                        )

                                                    [morphClass:protected] => 
                                                    [exists] => 1
                                                    [wasRecentlyCreated] => 
                                                )

                                            [1] => App\OrderDetail Object
                                                (
                                                    [fillable:protected] => Array
                                                        (
                                                            [0] => order_id
                                                            [1] => product_id
                                                            [2] => product_code
                                                            [3] => product_name
                                                            [4] => quantity
                                                            [5] => price
                                                        )

                                                    [connection:protected] => 
                                                    [table:protected] => 
                                                    [primaryKey:protected] => id
                                                    [perPage:protected] => 15
                                                    [incrementing] => 1
                                                    [timestamps] => 1
                                                    [attributes:protected] => Array
                                                        (
                                                            [id] => 55
                                                            [order_id] => 27
                                                            [product_id] => 6
                                                            [product_code] => 61
                                                            [product_name] => Cheddar Cheese-200
                                                            [quantity] => 1
                                                            [price] => 400
                                                            [created_at] => 2016-01-04 11:48:49
                                                            [updated_at] => 2016-01-04 11:48:49
                                                        )

                                                    [original:protected] => Array
                                                        (
                                                            [id] => 55
                                                            [order_id] => 27
                                                            [product_id] => 6
                                                            [product_code] => 61
                                                            [product_name] => Cheddar Cheese-200
                                                            [quantity] => 1
                                                            [price] => 400
                                                            [created_at] => 2016-01-04 11:48:49
                                                            [updated_at] => 2016-01-04 11:48:49
                                                        )

                                                    [relations:protected] => Array
                                                        (
                                                        )

                                                    [hidden:protected] => Array
                                                        (
                                                        )

                                                    [visible:protected] => Array
                                                        (
                                                        )

                                                    [appends:protected] => Array
                                                        (
                                                        )

                                                    [guarded:protected] => Array
                                                        (
                                                            [0] => *
                                                        )

                                                    [dates:protected] => Array
                                                        (
                                                        )

                                                    [dateFormat:protected] => 
                                                    [casts:protected] => Array
                                                        (
                                                        )

                                                    [touches:protected] => Array
                                                        (
                                                        )

                                                    [observables:protected] => Array
                                                        (
                                                        )

                                                    [with:protected] => Array
                                                        (
                                                        )

                                                    [morphClass:protected] => 
                                                    [exists] => 1
                                                    [wasRecentlyCreated] => 
                                                )

                                            [2] => App\OrderDetail Object
                                                (
                                                    [fillable:protected] => Array
                                                        (
                                                            [0] => order_id
                                                            [1] => product_id
                                                            [2] => product_code
                                                            [3] => product_name
                                                            [4] => quantity
                                                            [5] => price
                                                        )

                                                    [connection:protected] => 
                                                    [table:protected] => 
                                                    [primaryKey:protected] => id
                                                    [perPage:protected] => 15
                                                    [incrementing] => 1
                                                    [timestamps] => 1
                                                    [attributes:protected] => Array
                                                        (
                                                            [id] => 56
                                                            [order_id] => 27
                                                            [product_id] => 1
                                                            [product_code] => 1
                                                            [product_name] => Mango
                                                            [quantity] => 1
                                                            [price] => 90
                                                            [created_at] => 2016-01-04 11:48:49
                                                            [updated_at] => 2016-01-04 11:48:49
                                                        )

                                                    [original:protected] => Array
                                                        (
                                                            [id] => 56
                                                            [order_id] => 27
                                                            [product_id] => 1
                                                            [product_code] => 1
                                                            [product_name] => Mango
                                                            [quantity] => 1
                                                            [price] => 90
                                                            [created_at] => 2016-01-04 11:48:49
                                                            [updated_at] => 2016-01-04 11:48:49
                                                        )

                                                    [relations:protected] => Array
                                                        (
                                                        )

                                                    [hidden:protected] => Array
                                                        (
                                                        )

                                                    [visible:protected] => Array
                                                        (
                                                        )

                                                    [appends:protected] => Array
                                                        (
                                                        )

                                                    [guarded:protected] => Array
                                                        (
                                                            [0] => *
                                                        )

                                                    [dates:protected] => Array
                                                        (
                                                        )

                                                    [dateFormat:protected] => 
                                                    [casts:protected] => Array
                                                        (
                                                        )

                                                    [touches:protected] => Array
                                                        (
                                                        )

                                                    [observables:protected] => Array
                                                        (
                                                        )

                                                    [with:protected] => Array
                                                        (
                                                        )

                                                    [morphClass:protected] => 
                                                    [exists] => 1
                                                    [wasRecentlyCreated] => 
                                                )

                                        )

                                )

                        )

                    [hidden:protected] => Array
                        (
                        )

                    [visible:protected] => Array
                        (
                        )

                    [appends:protected] => Array
                        (
                        )

                    [guarded:protected] => Array
                        (
                            [0] => *
                        )

                    [dates:protected] => Array
                        (
                        )

                    [dateFormat:protected] => 
                    [casts:protected] => Array
                        (
                        )

                    [touches:protected] => Array
                        (
                        )

                    [observables:protected] => Array
                        (
                        )

                    [with:protected] => Array
                        (
                        )

                    [morphClass:protected] => 
                    [exists] => 1
                    [wasRecentlyCreated] => 
                )

        )

)

0 likes
5 replies
frezno's avatar

it's 'just' an array, ie you can loop through it by using foreach
what you've done so far, ie how does the appropiate method to get the collection look like and how do you pass it to the view? Do you use a shopping cart package and if so, which one?

skliche's avatar
skliche
Best Answer
Level 42

@eddy1992 Assuming your collection is stored in a variable called $orders:

for( $orders as $order ) {
    echo $order->id;
    echo $order->customer_id;
    echo $order->order_total;
    for( $order->orderDetails as $orderDetails ) {
        echo $orderDetails->product_name;
        echo $orderDetails->product_id;
        echo $orderDetails->quantity;
        echo $orderDetails->price;
    }
}

Likewise in any blade template.

frezno's avatar

@eddy1992

there's an example at github on the link you provided.

anyway in your method:

use Cart;
...
public function showCart()
{
    $cart = Cart::content();
    
    return view('shop.cart', compact('cart');
}

and in your shop/cart.blade.php

@foreach ($cart as $row)
    Name: {!! $row->name !!} <br>
    Quantity: {{ $row->qty }} <br>
    Price: {{ $row->price }} <br>
@endforeach

Please or to participate in this conversation.