0

I'am trying to fetch a session variable if the user is a guest. The variable is called "cart" and is set like this:

$product = new Collection((object) [
    'product_id' => $request->pId,
    'amount' => $request->amount,
    'variations' => $variations
]);

Session::push('cart', $product);

Then I later fetch it:

if(Auth::check()){
    $cartProducts = ShoppingCartItem::where('user_id', '=', Auth::user()->id)->get();
}else{
    $cartProducts = Session::get('cart');
}

foreach($cartProducts as $product){
    dd($product);
    $totalAmount += $product->amount;
    $totalPrice += (PriceHelper::getProductPrice($product->product->id, $product->amount));
}

The problem here is that dd($product) still outputs an array (the session variable array I assume) which means that for example $product->amount does not exist.

This is the output from dd($product):

enter image description here

2
  • So the problem would be that even though you are logged in Auth::check() returns false (thus leading to this issue)? Commented Jul 13, 2017 at 8:02
  • No, that works fine. I'm trying to fetch my products in the cart from the session when I'm logged out which works fine. What doesn't work is that I can't use $product->amount even though "amount" is in the $product variable Commented Jul 13, 2017 at 8:10

1 Answer 1

1

You can either access the values using get():

foreach ($cartProducts as $product) {
    $totalAmount += $product->get('amount');
    $totalPrice += PriceHelper::getProductPrice($product->get('product_id'), $product->get('amount'));
}

or as an array:

foreach ($cartProducts as $product) {
    $totalAmount += $product['amount'];
    $totalPrice += PriceHelper::getProductPrice($product['product_id'], $product['amount']);
}

or you could use sum() on the collection instead of using foreach:

$cartProducts = collect(Session::get('cart'));

$totalAmount = $cartProducts->sum('amount');
$totalPrice = $cartProducts->sum(function ($product) {
    return PriceHelper::getProductPrice($product['product_id'], $product['amount']);
});

Edit

For a quick fix if you need $product to be an object you could do something like:

$cartProducts = collect(Session::get('cart'))->map(function ($item) {
    return (object)$item->toArray();
});

Hope this helps!

Sign up to request clarification or add additional context in comments.

8 Comments

This works fine, thanks. But the problem is that I'd have to create two views (the cart view). One using for example $product->title (fproduct fetched from the database) and one using $product["title"] (product fetched from the session). Is it somehow possible to fetch values from that session array using the same notation ($product->title,...)?
@Scarwolf Ah, sorry I didn't realise you needed it to be an object. I've updated my answer.
Do you know if it is possbile to use the relationships somehow? For example my ShoppingCartItem Model has a relation to the product to fetch it's name, price etc. As I am saving the product id's in the Session is it somehow possible to do someting like $product->product->title where $product is not an Eloquent Model but some values including the product id stored in the session?
@Scarwolf Am I right in assuming that you have a ShoppingCart Model as well or is it just the ShoppingCartItem Model?
@Scarwolf You could potentially new-up a ShoppingCartItem for every product in the cart, populate it will data from the session (don't save it) and the Product relationship should work fine as it'll be a belongsTo relationship which won't need to the ShoppingCartItem id. If you have quite a few products in the session cart and you want it to be more efficient then you could manually gets all the Products from the cart and use setRelation() on the ShoppingCartItem model so you're not doing a separate query for every product.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.