首页 > 解决方案 > 我需要帮助

问题描述

你好为什么我会收到这个错误?

我需要一点帮助!

有错误码!

ErrorException 为 foreach() 提供的参数无效(查看:F:\server\htdocs\yii\yumypizza\resources\views\shop\shopping-cart.blade.php)

这是我来自 shop.shopping-cart 的代码

@extends('layouts.master')

@section('title')

THE YUMMI PIZZA
@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-toogle" 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>
@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

这是我的控制器功能代码。

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

和路线。

    Route::get('/add-to-cart/{id}', [
        'uses' => 'ProductController@getAddToCart',
        'as' => 'product.addToCart'


]);

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


]);

如果你能帮我解决这个问题!

标签: phplaravel-5

解决方案


在紧凑函数中发送变量

$contents = $this->someFunction();
return view('admin.category.List', compact('contents'));

推荐阅读