php - 试图在 Laravel 中使用 Gloudemans\Shoppingcart\ 获取非对象的属性“img”
问题描述
我正在创建一个 Laravel 电子商务网站,并且正在使用 Gloudemans\Shoppingcart\ 库。我遇到了“添加到购物车”按钮的问题。我再解释一下!我有一个 singleProduct.blade.php 文件,它从控制器调用产品信息:
这是将数据从数据库传递到视图的函数:
public function show($name)
{
$iamlush = iamlush::where('name', $name)->firstOrFail();
return view('singleProduct')->with('product', $iamlush);
}
这是 singleProduct 文件:
<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');
这是购物车控制器文件:
public function store(Request $request)
{
Cart::add($request->id, $request->name, 1, $request->price)->associate('App\iamlush');
return redirect()->route('cart.index')->with('success_message', 'Item was added to your cart!');
}
购物车页面作为几个 Laravel 部分。第一个是成功添加项目时传递成功消息:
@if (session()->has('success_message'))
<p class="itemAddedContainer">
{{ session()->get('success_message') }}
</p>
@endif
第二个是实际的购物车功能,它检查购物车的计数是否高于 0,即购物车中有物品:
@if(\Gloudemans\Shoppingcart\Facades\Cart::count() > 0)
<p class="introTxt">Looks like you have {{ \Gloudemans\Shoppingcart\Facades\Cart::count() }} Let's see what you have in your bag...</p>
<div class="rowContainer">
<!-- holds labels for cart -->
<div class="labelContainer">
<p>Product Image</p>
</div>
<div class="labelContainer">
<p>Name</p>
</div>
<div class="labelContainer">
<p>Quantity</p>
</div>
<div class="labelContainer">
<p>Price</p>
</div>
</div>
在本节之后,在 @else 和 @endif for 'count() > 0' 之前,是购物车中的 @foreach 循环:
@foreach( \Gloudemans\Shoppingcart\Facades\Cart::content() as $item)
<div class="productRowContainer">
<!-- holds labels for cart -->
<div class="cartItemContainer">
<img src="{{ asset('img/iamlush/shop/'.$item->model->img) }}" style="width: 30%;">
</div>
<div class="cartItemContainer">
<div class="dataContainer">
<img src="{{ asset('img/logo/'.$item->model->productLogo) }}" class="productLogo">
<p>Mediterranean Dark</p>
</div>
</div>
<div class="cartItemContainer">
<div class="dataContainer">
<div class="quantityContainer">
<button class="quantityBtn">-</button>
</div>
<div class="quantityContainer">
<p>1</p>
</div>
<div class="quantityContainer">
<button class="quantityBtn">+</button>
</div>
</div>
</div>
<div class="cartItemContainer">
<div class="dataContainer">
<p>{{ $item->model->presentprice() }}</p>
</div>
</div>
</div>
@endforeach
@else
<h3>You dont have anything</h3>
@endif
此部分将购物车的内容作为 $item,然后用于填充数据。我正在关注这个 youtube 教程:
Laravel 电子商务 - 购物车 - 第 2 部分 ( https://www.youtube.com/watch?v=Jzi6aLKVw-A&list=PLEhEHUEU3x5oPTli631ZX9cxl6cU_sDaR&index=4&t=430s )
在 13.00 分钟是他让它变得动态的时候,当我第一次尝试它时,它对我有用,但后来它突然停止工作,我不明白为什么,它给了我这个错误:
Trying to get property 'img' of non-object
解决方案
首先,尝试调试您的查询结果
dd($iamlush)
如果有空,检查你的数据库?你有那个名字的记录吗 可能有两个原因
- 你没有这种情况的任何记录
- 你的模型有错误
推荐阅读
- python - 处理大型 .asc 文件的最快方法是什么?
- python - 使用 pandas to_csv 将结果写入 csv
- sql - 为什么可以将 exec 的结果转储到表中,但不能将这些相同的结果用作子查询?
- php - 带有 PDFtk 的 Azure Web 应用程序(PHP laravel 后端)
- wordpress - 上一页 下一页 Wordpress 中自定义帖子类型的简码
- python - 为每个内容创建一个包含前 10 个推荐的数据框
- java - 从提取的 war 文件中动态加载外部类
- ruby-on-rails - 耙任务:NoMethodError:未定义的方法“标记”为 nil:NilClass
- excel - 将 PDF 文件路径和名称添加到单元格
- php - 为运输评论构建正则表达式