【问题标题】:I have problem with my code "UNDEFINED INDEX"我的代码“未定义索引”有问题
【发布时间】:2019-05-21 20:29:29
【问题描述】:

有链接到我以前的问题,代码在哪里,也许它可以帮助你 Parse error: syntax error, unexpected '__construct' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)

这是我遇到的问题

未定义索引:item(查看:C:\xampp\htdocs\laravel\resources\views\shoppingCart.blade.php) 这个错误在那里<strong><?php echo e($product['item']['name']); ?></strong>

这是我的路线

Route::get('shoppingCart', 'WebController@shoppingCart')->name('get.shoppingCart');

这是在我的网络控制器中

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

这是购物车的链接

<li><a href="{{ route('get.shoppingCart') }}">CART  <span class="badge">{{Session::has('cart') ? '!' : ''}}</span></a></li>

这是我的购物车代码

@extends('layouts.main')
@section('content')
@if(Session::has('cart'))
    <div>
        <div>
            <ul class="list-group">
                @foreach($products as $product)
                <li class="list-group-item"></li>
                <strong>{{$product['item']['name']}}</strong>
                <li><a href="#">Odstrániť</a></li>
                @endforeach
            </ul>
        </div>
    </div>

@else
<div>
    <h2>V košíku nemáš žiadne položky!!!</h2>
</div>
@endif

@endsection

【问题讨论】:

  • 您好,您应该提供回答您所需的所有详细信息,因为这是另一个问题。至少是发生错误的代码行,以及完整的错误(哪个索引未定义?)
  • @Angelika Beňová:无法理解你的问题
  • @AngelikaBeňová 如果遇到新错误,请根据新错误提问。您可以编辑您的问题,并根据错误输入正确的所有详细信息,然后我们可以轻松为您提供帮助
  • 谢谢我编辑这个问题

标签: laravel indexing undefined


【解决方案1】:

首先,您可以在编写 dd($cart->items); 时检查 $cart->items 上是否有数据。关于你的功能。关注我的cmets。

public function shoppingCart(){
  if(!Session::has('cart')){
    return view('shoppingCart');
  }
  $oldCart = Session::get('cart');
  $cart = new Cart($oldCart);

 //dd($cart->items) /*uncomment this and check if you have data or not if you have no then problem is  */
  return view('shoppingCart',['products'=>$cart->items]); /* on the other if you have data dont forget those line and look at my comment on blade.php */
}

你的blade.php

@extends('layouts.main')
@section('content')
@if(Session::has('cart'))
    <div>
        <div>
            <ul class="list-group">
                @foreach($products as $product)
                <li class="list-group-item"></li>
                <strong>{{$product['item']['name']}}</strong>



<!--Look at here you remembe you were add products writing `['products'=>$cart->items]` then you be care calling your datas first you be relay on your codes you are working array (it seems you are working with objects then you need to call it $products->item->name Secondly you remember on the function you were add your data `['products'=>$cart->items]` like this if there is no child items object on $cart->items then you need to call it like 

        foreach($products as $product))
        {
         echo $product->name; //if it is array then echo $product['name'];
        }

    -->
                    <li><a href="#">Odstrániť</a></li>
                    @endforeach
                </ul>
            </div>
        </div>

    @else
    <div>
        <h2>V košíku nemáš žiadne položky!!!</h2>
    </div>
    @endif

    @endsection

【讨论】:

    猜你喜欢
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    相关资源
    最近更新 更多