I developed a laravel project. When I copy this project to another local server, all are running fine but only one page that gives me an error like: Trying to get property of non-object. Strange things is, where i developed this project all are fine.
Here is my code which actually give me the error:
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]);
}
Here is my view file:
@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
Here is my route file:
Route::get('/addProduct/{productId}', 'CartController@addItem')-
>name('addcart');
Route::get('/removeItem/{productId}', 'CartController@removeItem');
Route::get('/cart', 'CartController@showCart')->name('cart');
When route:/cart I got an error in other laptop,(error screenshot added below) but in my PC it works fine. Please help me. Thanks in advance. enter image description here
it is becuse you are calling $cart->cartItems; what if you do not have cart items or say following part is executed;
this will create new cart without any cart items so your $items variable will be null and calling foreach on null will return error...
on your own pc you might have already added a cart with items that is why you are not getting any error but with fresh install you will get error till you add any cart with cartItems