有一个指向我之前的问题的链接,代码在哪里,也许可以帮助您 Parse error: syntax error, unexpected '__construct' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)
这是我遇到的问题
未定义索引:项目(视图:C:\ xampp \ htdocs \ laravel \ resources \ views \ shoppingCart.blade.php)
存在此错误serve
这是我的路线
<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]);
}
这是我的shoppingCart代码
<li><a href="{{ route('get.shoppingCart') }}">CART <span class="badge">{{Session::has('cart') ? '!' : ''}}</span></a></li>
答案 0 :(得分:0)
首先,您可以在编写dd($ cart-> items)时检查$ cart-> items上是否有数据;在您的功能上。跟随我的评论。
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