Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...