Skip to content
Advertisement

Laravel Applications Invalid argument supplied for foreach() Errors

I am learning PHP Laravel and I am developing shopping cart project. I am able to add the product into list but, when I click shopping cart link. I am getting following error.

FacadeIgnitionExceptionsViewException Invalid argument supplied for foreach() (View: C:UsersKhundokar NirjorDesktoplaravel Tutorialshopping-cartresourcesviewsshopshopping-cart.blade.php)

Here is my Router

Route::get('/shopping-cart',[
    'uses' => 'ProductController@getCart',
    'as' => 'product.shopppingCart'
]);

Here is my header.blade.php code.

<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">Brand</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="{{route ('product.shopppingCart')}}"><i class="fa fa-shopping-cart" aria-hidden="true"></i> Shopping Cart
                <span class="badge">{{ Session::has('cart') ? Session::get('cart')->totalQty : ''  }}</span>
                </a>
                </li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><i class="fa fa-user" aria-hidden="true"></i> User Mangemetn <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                    @if(Auth:: check())
                    <li><a href="{{ route('user.profile')}}">User Profile</a></li>
                    <li><a href="{{ route('user.logout')}}">Logout</a></li>
                    @else
                    <li><a href="{{ route('user.signup')}}">Sign Up</a></li>
                   <li><a href="{{ route('user.signin')}}">Sign In</a></li>
                    @endif


                        <li role="separator" class="divider"></li>

                    </ul>
                </li>
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>

Here is my shopping-cart.blade.php

@extends('layouts.master')

@section('title')
    Laravel Shopping Cart
@endsection

@section('content')
     @if(Session::has('cart'))
     <div class="row">
     <div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
     <ul class="list-group">
     @foreach($products as $product) 
     <li class ="list-group-item">
     <span class ="badge"> {{ $product['qty'] }}</span>
     <strong >{{ $product['item']['title']}}</strong>
     <span class ="label label-success"> {{ $product['price']}}</span>
     <div class="btn-group">
     <button type="button" class="btn btn-primary btn-xs dropdown-toggle" data-toggle="dropdown">Action<span class="caret"></span></button>
     <ul class="dropdown-menu">
     <li> <a href="#"> Reduce By 1 </a> </li>
     <li> <a href="#"> Reduce All </a> </li>
     </ul>
     </div>
     </li>
     @endforeach
     </ul>
     </div>
     </div>

     <div class ="row">
     <div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
     <strong > Total : {{ $totalPrice}}</strong>
     </div>
     </div>

      <hr>

     <div class ="row">
     <div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
     <button type="button" class="btn btn-success">Checkout</button>
     </div>
     </div>


     <div class ="row">
     <div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
     <strong>Total : {{$totalPrice}} </strong>
     </div>
     </div>
     <hr>
     <div class ="row">
     <div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
       <button type="button" class ="btn-btn-success">Checkout</button>
     </div>
     </div>

    @else
     <div class ="row">
     <div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
     <h2>No Items In Cart</h2>
     </div>
     </div>


     @endif
@endsection

Here is my controller code

public function getCart()
    {
        if(!Session :: has('cart') ){
            return view ('shop.shopping-cart');
        }
        $oldCart = Session::get('cart');
        $cart = new Cart($oldCart);
        return view ('shop.shopping-cart' , ['products' => $cart->items, 'totalPrice'=>$cart->totalPrice]);
    }

Here is screen shot when I clicked the shopping-cart link

enter image description here

Advertisement

Answer

Try like this

 @if(!empty($products) && count($products) > 0) 
     @foreach($products as $product)
       // your code here
     @endforeach // typo in foreach fixed
 @endif
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement