Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

php - Laravel 8: Trying to get property 'price' of non-object

I'm trying to make an online store with Laravel 8 and for my cart.blade.php I coded this:

<div class="text-right mt-4">
    <label class="text-muted font-weight-normal m-0">Final Price</label>

    @php
        $totalPrice = Cart::all()->sum(function($cart) {
        return $cart['product']->price * $cart['quantity'];
        });
    @endphp
    <div class="text-large"><strong>{{ $totalPrice }} is price</strong></div>
</div>

And method all() goes like this:

public function all()
    {
        $cart = $this->cart;
        $cart = $cart->map(function($item) {
            return $this->withRelationshipIfExist($item);
        });

        return $cart;
    }

So how to fix this issue? I would really appreciate any idea or suggestion from you guys...

Thanks in advance.

question from:https://stackoverflow.com/questions/66068191/laravel-8-trying-to-get-property-price-of-non-object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The error is telling you that $cart is not an object. This happens when it is not being set correctly.

If your all() function is in your Cart model, then you actually don't want $this->cart. You would simply want $cart = $this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...