尝试在 Laravel 中使用 Gloudemans\Shoppingcart\ 获取非对象的属性

我正在创建一个 Laravel 电子商务网站,并且正在使用 Gloudemans\Shoppingcart\ 库。我在使用“添加到购物车”按钮时遇到问题。我再解释一下!我有一个 singleProduct.blade.php 文件,它从控制器调用产品信息:


这是将数据从数据库传递到视图的函数:


public function show($name)

{

    $iamlush = iamlush::where('name', $name)->firstOrFail();


    return view('singleProduct')->with('product', $iamlush);

}

这是单个产品文件:


<div class="contentContainer">

    <div class="rowContainer text-center">

        <div class="productImgContainer">

            <img src="{{ asset('img/iamlush/shop/'.$product->img) }}" class="productImg">

        </div>


        <div class="productInfoContainer">

            <div class="text-center">

                <div class="productLogoContainer">

                    <img src="{{ asset('img/logo/'.$product->productLogo) }}" class="logoContainer">

                </div>


                <h3>{{ $product->name }}</h3>

                <h5>{{ $product->priceFormat() }}</h5>

             </div>


             <p class="descriptionContainer">

                 {{ $product->description }}

             </p>


             <div class="rowContainer">

                 <div class="iconContainer">

                     <img src="{{ asset('img/productPage/productInfo.png') }}" class="icons">

                 </div>


             <div class="iconContainer">

                 <img src="{{ asset('img/productPage/paymentMethods.jpg') }}" class="icons">

             </div>

         </div>


         <form action="{{ route('cart.store') }}" method="POST" class="formContainer">

             @csrf

             <input type="hidden" name="id" value="{{ $product->id }}">

             <input type="hidden" name="name" value="{{ $product->name }}">

             <input type="hidden" name="price" value="{{ $product->price }}">


             <div class="BtnContainer">

                 <button type="submit" class="btnCart">Add To Cart</button>

             </div>

         </form>

        </div>

    </div>

</div>

“添加到购物车”向“cart.store”发送 POST 请求,这是 web.php:


Route::post('/cart', 'CartController@store')->name('cart.store');


白板的微信
浏览 79回答 1
1回答

慕莱坞森

首先,尝试调试您的查询结果dd($iamlush)如果有null,检查你的数据库?您有该名称的任何记录吗?可能有两个原因您没有此情况的任何记录你的模型有错误
打开App,查看更多内容
随时随地看视频慕课网APP