为什么要尝试获取非对象的属性但是另一台计算机可以正常工作?

时间:2017-09-13 14:41:22

标签: php laravel laravel-5 laragon

我在我的电脑上开发了一个laravel项目。当我将这个项目复制到另一台笔记本电脑并运行一切都很好,但只有一个页面给我一个错误,如:尝试获取非对象的属性。奇怪的事情在我的PC上我开发这个项目都很好。我完全陷入困境。请帮帮我。

这是我的代码实际上给了我错误:

CartController:

     public function showCart(){
    $cart = Cart::where('user_id',Auth::user()->id)->first();
    //dd($cart);

    if(!$cart){
        $cart =  new Cart();
        $cart->user_id=Auth::user()->id;
        $cart->save();
    }

    $items = $cart->cartItems;
    $total=0;
   // dd($items);
    foreach($items as $item){
        $total+=$item->product->price;
    }

    return view('cart.view',['items'=>$items,'total'=>$total]);
}

以下是我的观看文件:

   @section('content')

<div class="row">
    <div class="col-sm-12 col-md-10 col-md-offset-1">
        <table class="table table-hover">
            <thead>
            <tr>
                <th>Product</th>
                <th></th>
                <th class="text-center"></th>
                <th class="text-center">Total</th>
                <th> </th>
            </tr>
            </thead>
            <tbody>
            @foreach($items as $item)
                <tr>
                    <td class="col-sm-8 col-md-6">
                        <div class="media">
                            <a class="thumbnail pull-left" href="#"> <img class="media-object" src="{{asset("/images"."/".$item->product->picturePath)}}" style="width: 100px; height: 72px;"> </a>
                            <div class="media-body">
                                <h4 class="media-heading"><a href="#">{{$item->product->name}}</a></h4>
                            </div>
                        </div></td>
                    <td class="col-sm-1 col-md-1" style="text-align: center">
                    </td>
                    <td class="col-sm-1 col-md-1 text-center"></td>
                    <td class="col-sm-1 col-md-1 text-center"><strong>{{$item->product->price}}Tk</strong></td>
                    <td class="col-sm-1 col-md-1">
                        <a href="/removeItem/{{$item->id}}"> <button type="button" class="btn btn-danger">
                                <span class="fa fa-remove"></span> Remove
                            </button>
                        </a>
                    </td>
                </tr>
            @endforeach

            <tr>
                <td>   </td>
                <td>   </td>
                <td>   </td>
                <td><h3>Total</h3></td>
                <td class="text-right"><h3><strong>{{$total}}TK</strong></h3></td>
            </tr>
            <tr>
                <td>   </td>
                <td>   </td>
                <td>   </td>
                <td>
                    <a href="/"> <button type="button" class="btn btn-default">
                            <span class="fa fa-shopping-cart"></span> Continue Shopping
                        </button>
                    </a></td>

                <form action="{{route('checkout.view')}}" method="post">
                    {{csrf_field()}}
                    <input type="hidden" value="{{$total}}" name="total">
                    <td>
                       <button type="submit" class="btn btn-success">
                                Checkout <span class="fa fa-play"></span>
                            </button></td>
                </form>

            </tr>
            </tbody>
        </table>
    </div>
</div>@endsection

这是我的路线档案:

    Route::get('/addProduct/{productId}', 'CartController@addItem')-
    >name('addcart');
    Route::get('/removeItem/{productId}', 'CartController@removeItem');
    Route::get('/cart', 'CartController@showCart')->name('cart');

当路由:/ cart我在其他笔记本电脑上出现错误时,(在下面添加了错误截图)但在我的电脑中它工作正常。请帮我。提前致谢。 enter image description here

1 个答案:

答案 0 :(得分:0)

这是因为你打电话给$ cart-&gt; cartItems;如果您没有购物车物品或者说下列部件已执行,该怎么办;

if(!$cart){
    $cart =  new Cart();
    $cart->user_id=Auth::user()->id;
    $cart->save();
}

这将创建没有任何购物车商品的新购物车,因此您的$ items变量将为null并且调用foreach on null将返回错误...

在您自己的电脑上,您可能已经添加了任何带有项目的购物车,这就是为什么您没有收到任何错误但是在全新安装的情况下,您将收到错误,直到您添加任何带有cartItems的购物车