为什么我会收到此错误:未定义的变量:产品

我是 Laravel 的新手,在构建项目时我遇到了错误:


未定义变量:产品(查看:C:\xampp\htdocs\basic\resources\views\mycart.blade.php)


检查错误以修复后仍然存在,我希望你们能帮助我。提前致谢


mycart刀片代码:


@extends('layouts.app')


@section('content')

@if(Session::has('cart'))

<div class="container">

    <b class="text-uppercase" style="color: darkgrey; font-size: 18px;">Item(s)</b>

<div class="row">

            @foreach($products as $products)

                <div class="col-sm-12 col-lg-2" style="padding-bottom: 12%;">

                    

                        <div class="cart-item-container">

                            <a href="ads/{{strtolower(str_replace(" ","-",$product->name))}}"><div class="item-image"><img src="{{asset($product->image)}}" class="item-image">

                            </div>

                            <div class="item-tag" >

                                <p class="h5 text-capitalize">{{$product->name}}</p></a>

                                <p class="h4">GH¢ {{$product->price}}</p>

                                <div class="form-group" style="float: left; width: 25%;">

                                <select class="form-control" id="exampleFormControlSelect1">

      <option>1</option>

      <option>2</option>

      <option>3</option>

      <option>4</option>

      <option>5</option>

      <option>6</option>

      <option>7</option>

      <option>8</option>

      <option>9</option>

      <option>10</option>

    </select>

  </div>

    <div style="float: right;"><ul class="list-inline">

                                <li><a href="#" title="Save item for future use"><p class="text-primary text-capitalize"><span class="glyphicon glyphicon-heart-empty"></span></span></a></li>

                                <li><a href="#" title="Remove item from cart"><p class="text-muted text-capitalize"><span class="glyphicon glyphicon-trash"></span></p></a></li>

                            </ul>

                        </div>

                            </div>  

                        </div>  

                </div>

            @endforeach

</div>


幕布斯6054654
浏览 102回答 2
2回答

慕码人2483693

在您的getCart()方法中,如果有Session变量,您将返回产品cart。但是,如果没有 Session cart variable,您将返回没有附加任何变量的视图:if (!Session::has('cart')) {&nbsp; &nbsp; return view('mycart');}要修复刀片视图中缺少变量的错误,请定义一个空值$products并将其与控制器中的视图一起返回:if (!Session::has('cart')) {&nbsp; &nbsp; $products = [];&nbsp; &nbsp; $totalPrice = 0;&nbsp; &nbsp; return view('mycart', compact('products', 'totalPrice));}&nbsp;注意 - 你会遇到同样的问题,$totalPrice所以我为该变量定义了 0 以防止错误。

元芳怎么了

<?php&nbsp;&nbsp;namespace App\Http\Controllers;&nbsp;&nbsp;use App\Product;use Illuminate\Http\Request;&nbsp;&nbsp;class ProductController extends Controller{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Display a listing of the resource.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return \Illuminate\Http\Response&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function index()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $products = Product::latest()->paginate(5);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return view('products.index',compact('products'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->with('i', (request()->input('page', 1) - 1) * 5);&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Show the form for creating a new resource.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return \Illuminate\Http\Response&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function create()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return view('products.create');&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Store a newly created resource in storage.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \Illuminate\Http\Request&nbsp; $request&nbsp; &nbsp; &nbsp;* @return \Illuminate\Http\Response&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function store(Request $request)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $request->validate([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name' => 'required',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'detail' => 'required',&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Product::create($request->all());&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return redirect()->route('products.index')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->with('success','Product created successfully.');&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Display the specified resource.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \App\Product&nbsp; $product&nbsp; &nbsp; &nbsp;* @return \Illuminate\Http\Response&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function show(Product $product)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return view('products.show',compact('products'));&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Show the form for editing the specified resource.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \App\Product&nbsp; $product&nbsp; &nbsp; &nbsp;* @return \Illuminate\Http\Response&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function edit(Product $product)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return view('products.edit',compact('product'));&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Update the specified resource in storage.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \Illuminate\Http\Request&nbsp; $request&nbsp; &nbsp; &nbsp;* @param&nbsp; \App\Product&nbsp; $product&nbsp; &nbsp; &nbsp;* @return \Illuminate\Http\Response&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function update(Request $request, Product $product)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $request->validate([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name' => 'required',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'detail' => 'required',&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $product->update($request->all());&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return redirect()->route('products.index')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->with('success','Product updated successfully');&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Remove the specified resource from storage.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \App\Product&nbsp; $product&nbsp; &nbsp; &nbsp;* @return \Illuminate\Http\Response&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function destroy(Product $product)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $product->delete();&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return redirect()->route('products.index')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->with('success','Product deleted successfully');&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP