为什么试图获得非对象的属性但工作正常的另一台计算机?

问题描述:

我在PC上开发了一个laravel项目。当我将这个项目复制到另一台笔记本电脑,并运行所有都很好,但只有一个页面,给我一个错误,如:试图获得非对象的属性。奇怪的事情发生在我的个人电脑里,我开发这个项目都很好。我完全卡住了。请帮助我。为什么试图获得非对象的属性但工作正常的另一台计算机?

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

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}', '[email protected]')- 
    >name('addcart'); 
    Route::get('/removeItem/{productId}', '[email protected]'); 
    Route::get('/cart', '[email protected]')->name('cart'); 

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

+0

什么是62线?我的猜测:这里展示的第二个,对吧?第62行的 – Jeff

+0

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

这是你打电话给$ cart-> cartItems;如果你没有购物车项目或者说下面的部分被执行,那该怎么办?

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

这将在自己的电脑建立新的购物车没有任何车项目,让您$项目变量将是无效的,并呼吁空的foreach将返回错误...

你可能已经被添加到购物车与项目,这就是为什么你没有得到任何错误,但与全新安装,你会得到错误,直到你添加cartItems的任何购物车