mercredi 13 septembre 2017

why Trying to get property of non-object But work fine another Computer?

I developed a laravel project in my pc.when i copy this project to another laptop and run all are fine but only one page that gives me an error like:Trying to get property of non-object.Strange things is in my pc where i developed this project all are fine. I am totally stuck.Please help me on this. Here i provide 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="" style="width: 100px; height: 72px;"> </a>
                            <div class="media-body">
                                <h4 class="media-heading"><a href="#"></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>Tk</strong></td>
                    <td class="col-sm-1 col-md-1">
                        <a href="/removeItem/"> <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>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="" method="post">
                    
                    <input type="hidden" value="" 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 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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire