我开发了一个 laravel 项目。当我将此项目复制到另一台本地服务器时,一切都运行良好,但只有一个页面给我一个错误,例如:Trying to get property of non-object。奇怪的是,我开发这个项目的地方都很好。
这是我的代码,实际上给了我错误:
购物车控制器:
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');
当 route:/cart 我在其他笔记本电脑中遇到错误,(错误截图添加在下面)但在我的 PC 中它工作正常。请帮我。提前致谢。 在此处输入图像描述