【发布时间】:2018-08-20 15:36:16
【问题描述】:
你能帮我解决这个问题吗.. 我正在尝试显示 foreach 数组中的值.. 关系让我更难理解,所以我选择按我的方式做。
显示类似这样的错误
此集合实例上不存在属性 [pic]。
$dataTagged = DB::table('tagging_tagged')
->where('tag_slug', '=',$slug)
->get();
foreach($dataTagged as $tagged){
$dataProductquantity[] = Productquantity::where('id','=',$tagged->taggable_id)->first();
}
我的观点是
@foreach($dataProductquantity as $Productquantity)
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<a href="#"><img class="card-img-top" src="{{ asset('productimg') }}/{{$Productquantity->pic}}" alt=""></a>
<div class="card-body">
<h4 class="card-title">
<a href="#">{{$Productquantity->product->product_name}}</a>
</h4>
<h5>₱ {{$Productquantity->price}}.00 </h5>
<p class="card-text">{{$Productquantity->description}}</p>
</div>
<div class="card-footer" align="center">
<a class="btn btn-warning" align="center"><i class="icon-shopping-cart"></i><span style="color:#fff;">Add to Cart</span></a>
</div>
</div>
</div>
@endforeach
请帮忙。谢谢
【问题讨论】:
-
您的错误消息实际上足以告诉您自己解决此问题。但是...不确定如何构建模型,但您可能应该使用
$Productquantity->product->pic,因为恕我直言,在Productquantity模型上拥有pic属性是没有意义的。只需仔细检查pic的正确位置。不过,您还应该检查为什么视图中的$Productquantity是一个集合而不是模型本身。
标签: php mysql laravel eloquent relationships