laravelshoppingcart icon indicating copy to clipboard operation
laravelshoppingcart copied to clipboard

How to work with associatedModel

Open tomforjerry23 opened this issue 4 years ago • 2 comments

Hello, I am using associatedModel to add cart, below I show my code any returning array from CartCollection Object. But I can't get MenuSectionItem value, he return error, please tell me how to get associatedModel value from data.

$item = MenuSectionItem::findOrFail(5714367);
$cartDataArray = array(
         'id' => $item->id,
         'name' => $item->name_english,
         'price' => $item->price,
         'quantity' => '1',
         'attributes' => array(),
         'associatedModel' => $item
   );
\Cart::add($cartDataArray);


$cart_items = \Cart::getContent();
foreach($cart_items as $cart_item){
   echo $cart_item->associatedModel->name_english;
}

Darryldecode\Cart\CartCollection Object
(
    [items:protected] => Array
        (
        [367] => Darryldecode\Cart\ItemCollection Object
            (
                [config:protected] => Array
                    (
                        [format_numbers] => 
                        [decimals] => 0
                        [dec_point] => .
                        [thousands_sep] => ,
                        [storage] => 
                        [events] => 
                    )
                [items:protected] => Array
                    (
                        [id] => 367
                        [name] => Chicken Fried Noodle
                        [price] => 2.3
                        [quantity] => 2
                        [attributes] => Darryldecode\Cart\ItemAttributeCollection Object
                            (
                                [items:protected] => Array
                                    (
                                    )
                            )
                        [conditions] => Array
                            (
                            )
                    )
            )
        [5714367] => Darryldecode\Cart\ItemCollection Object
            (
                [config:protected] => Array
                    (
                        [format_numbers] => 
                        [decimals] => 0
                        [dec_point] => .
                        [thousands_sep] => ,
                        [storage] => 
                        [events] => 
                    )

                [items:protected] => Array
                    (
                        [id] => 5714367
                        [name] => Chicken Fried Noodle
                        [price] => 2.3
                        [quantity] => 11
                        [attributes] => Darryldecode\Cart\ItemAttributeCollection Object
                            (
                                [items:protected] => Array
                                    (
                                    )

                            )
                        [conditions] => Array
                            (
                            )
                        [associatedModel] => App\Model\MenuSectionItem Object
                            (
                                [fillable:protected] => Array
                                    (
                                        [0] => menu_section_id
                                        [1] => name_english
                                        [2] => name_arabic
                                        [3] => description_english
                                        [4] => description_arabic
                                        [5] => price
                                        [6] => sort_order
                                        [7] => status
                                        [8] => thumbnail
                                    )
                                [connection:protected] => mysql
                                [table:protected] => menu_section_items
                                [primaryKey:protected] => id
                                [keyType:protected] => int
                                [incrementing] => 1
                                [with:protected] => Array
                                    (
                                    )
                                [withCount:protected] => Array
                                    (
                                    )
                                [perPage:protected] => 15
                                [exists] => 1
                                [wasRecentlyCreated] => 
                                [attributes:protected] => Array
                                    (
                                        [id] => 367
                                        [menu_section_id] => 36
                                        [name_english] => Chicken Fried Noodle
                                        [name_arabic] => نودلز مع الدجاج المقلي
                                        [description_english] => stir fried noodles With chicken & vegetables. deliciously simple flavors.
                                        [description_arabic] => نودلز مع الدجاج المقلي
                                        [price] => 2.300
                                        [sort_order] => 16
                                        [status] => active
                                        [thumbnail] => default.png
                                        [deleted_at] => 
                                        [created_at] => 2019-08-11 10:47:34
                                        [updated_at] => 2019-09-10 00:51:47
                                    )
                                [original:protected] => Array
                                    (
                                        [id] => 367
                                        [menu_section_id] => 36
                                        [name_english] => Chicken Fried Noodle
                                        [name_arabic] => نودلز مع الدجاج المقلي
                                        [description_english] => stir fried noodles With chicken & vegetables. deliciously simple flavors.
                                        [description_arabic] => نودلز مع الدجاج المقلي
                                        [price] => 2.300
                                        [sort_order] => 16
                                        [status] => active
                                        [thumbnail] => default.png
                                        [deleted_at] => 
                                        [created_at] => 2019-08-11 10:47:34
                                        [updated_at] => 2019-09-10 00:51:47
                                    )
                                [changes:protected] => Array
                                    (
                                    )
                                [casts:protected] => Array
                                    (
                                    )
                                [dates:protected] => Array
                                    (
                                        [0] => deleted_at
                                    )
                                [dateFormat:protected] => 
                                [appends:protected] => Array
                                    (
                                    )
                                [dispatchesEvents:protected] => Array
                                    (
                                    )
                                [observables:protected] => Array
                                    (
                                    )
                                [relations:protected] => Array
                                    (
                                    )
                                [touches:protected] => Array
                                    (
                                    )
                                [timestamps] => 1
                                [hidden:protected] => Array
                                    (
                                    )
                                [visible:protected] => Array
                                    (
                                    )
                                [guarded:protected] => Array
                                    (
                                        [0] => *
                                    )
                                [forceDeleting:protected] => 
                            )
                    )
            )
    )
)

tomforjerry23 avatar Mar 28 '20 15:03 tomforjerry23

you can't add item to cart and run the method this method \Cart::getContent(); at same time.

use a different method for adding and fetching

techbly avatar Mar 29 '20 20:03 techbly

The documentation says in the link that you access the Model by making $item-> model. This does not work because the associated method adds the model in this way $item['AssociatedModel'] = $model;. As you can see your own code, the Model you associated has been added to the 'associatedModel' matrix item and you access it by doing

foreach(Cart::getContent() as $row) { echo $row->associatedModel->price; }

if you want to access the 'price' value for example.

As shown in the documentation example, I can't do it. For example, my Model is Product and I want to access the price value.

$add = \Cart::session(\Auth::id())->add($item);
$product = Product::findOrFail(1);
$add->associate($product);

foreach(Cart::getContent() as $row) {
            echo $row->product->price;
        }

And I get the error ErrorException: Trying to get property 'price' of non-object.

webfelipemaia avatar Jul 06 '20 11:07 webfelipemaia